栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java学习总结5

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java学习总结5

1.Object类

        Object是类Object结构的根。 每个类都有Object作为超类(父类).
        常用的功能:大部分常用类已经将这些功能重写了
                 public final Class getClass():表示正在运行的类的实例   (当前类的字节码文件对象)
                public int hashCode():哈希码值,把它可以看成是一个"地址值",不是真实意义的地址值,和哈希表有关,不同的对象,哈希码值不同.
        获取一个类的字节码文件对象的方式有几种?
                1)Object的方法 public final Class getClass()
                2)任意Java类型的class属性

public class ObjectDemo {
    public static void main(String[] args) {
   // Demo demo = new Demo() ;
     //   System.out.println(demo.getClass()) ;
        //创建两个学生对象
        Student s1 = new Student() ;
        Student s2 = new Student() ;
        //System.out.println(s1) ;
        //System.out.println(s2) ;
        Class c1 = s1.getClass() ;
        Class c2 = s2.getClass() ;

        System.out.println("-----------------------------------");
        // public String getName()
        String name1 = c1.getName();
        String name2 = c2.getName();
        System.out.println(name1+"----"+name2);

        System.out.println(c1) ;//包名.类名
        System.out.println(c2) ;
        System.out.println(s1==s2) ;//分别需要开辟空间
        System.out.println(c1==c2) ; //因为类就加载一次,产生的字节码文件就一个

        System.out.println("----------------------------------") ;
        //另一种方式也可以获取字节码文件对象
        //任意Java类型的class属性(自定义的类/jdk提供类)
        Class c3 = Student.class ;
        System.out.println(c3);
        System.out.println(c3==c2);
        System.out.println("----------------------------------") ;

        int i = s1.hashCode();
        int i2 = s2.hashCode();
        System.out.println(i);
        System.out.println(i2);

        System.out.println("高圆圆".hashCode()) ;
        System.out.println("张少华".hashCode());
    }
}
1.1 public String toString()

        返回对象的字符串表示形式
        应该是一个简明扼要的表达,容易让人阅读.创建对象,输出对象名称.打印出来的对象的地址值,没有意义;应该看到的是当前这个对象的成员信息表达式.建议所有子类覆盖此方法.

 1.2 public boolean equals(Object obj)

        指示其他对象与此对象是否"相等"
        ==和equals的区别:
                ==:连接的是基本数据类型:比较的基本数据类型数据值是否相等
                        int a= 10 ;
                        int b = 20 ;
                        a==b ;
                ==:连接的是引用数据类型,比较的是引用数据类型的地址值是否相同
               equals:默认比较的是地址值是否相同,建议子类重写equals方法,比较他们内容是否相同(成员信息是否相同).
                在重写equals方法的同时,还需要重写hashCode()方法,保证哈希码值必须一样,才能比      较equals.
                大部分的常用类都会重写Object类的equals方法.

1.3 protected Object clone() throws CloneNotSupportedException

        调用过程中可能存在克隆不支持的异常
        对于jdk提供的类的方法本身存在异常.谁调用这个方法,必须做出处理,否则报错,最简单的方式继续往上抛 throws

2.Scanner类

文本扫描器
        构造方法
        public Scanner(InputStream source)
        成员方法:
                判断功能 haxNextXXX():判断下一个录入都是是否是XXX类型
                public boolean hasNextInt():
                public boolean hasNextLine().
                获取功能    nextXXX()
                                  public int nextInt()
                                  public String nextLine()
                                  public String next()

public class ScannerDemo {

    public static void main(String[] args) {

        //public Scanner(InputStream source):形式参数时候一个抽象类,需要有子类对象
        //System类中存在一个"标准输入流"in
        //public static final InputStream in
        InputStream inputStream = System.in ; //字节输入流


        //创建键盘录入对象
        Scanner sc = new Scanner(inputStream) ;

        System.out.println("录入一个数据:");



       
        //java.util.InputMismatchException:录入的数据和需要接收的num类型不匹配
        //Scanner类的提供的判断功能
        if(sc.hasNextInt()){
            //录入的int类型
            //接收int
            int num = sc.nextInt() ;
            System.out.println("您输入的数据是:"+num);
        }else if(sc.hasNextLine()){
            //录入的String
            String line = sc.nextLine() ;
            System.out.println("您输入的数据是:"+line);
        }else{
            System.out.println("对不起,没有提供其他接收的类型值...");
        }

    }
}
3.String类

        String类代表字符串
        Java程序中的所有字符串文字(例如"abc" )都被实现为此类的实例。 "字符串本身就是常量"
        特点:字符串是不变的,它们的值在创建后不能被更改.
        构造方法:
                public String():无参构造
                String(byte[] bytes):将字节数组可以构造字符串对象
                public String(byte[] bytes,int offset,int length)一部分字节数组构造成字符串对象
                public String(char[] value):将字符数组构造成字符串对象
                public String(char[] value, int offset,int count):将一部分字符数组构造成字符串对象
                public String(String original):创建一个字符串对象,里面存储字符串常量
        String s = "hello" 和String s2 = new String("hello") ;两个有什么区别,分别创建了几个对象.
                共同点:都是在创建一个字符串对象"hello",但是内存执行不一样.
                前者:只是在常量池中开辟空间,创建一个对象  :推荐这种方式,节省内存空间.
                后者:在堆中开辟空间,而且里面存在常量---常量池中标记 ,创建两个对象.
               

3.1 String类的获取功能

        char charAt(int index)  :获取指定索引出的字符值
        public String concat(String str):拼接功能:拼接新的字符串
        public int indexOf(int ch):返回指定字符第一次出现的字符串内的索引
        int lastIndexOf(int ch)  :查询指定字符最后一次出现的索引值
        int length()  :获取字符串长度
        public String[] split(String regex):字符串的拆分方法:返回的字符串数组
        String substring(int beginIndex) :字符串截取功能  默认从beginIndex起始索引截取到末尾
        public String substring(int beginIndex,int endIndex):从指定位置开始截取到指定位置结束(包前不包后)包含beginIndex位置,不包含endIndex位置,包含到endIndex-1

public class StringDemo {
    public static void main(String[] args) {

        //测试
        //创建一个字符串
        String s = "helloworldJavaEE" ;
        //  char charAt(int index)  :获取指定索引出的字符值
        System.out.println("charAt():"+s.charAt(4)) ;
        System.out.println("--------------------------------------") ;
        // public String concat(String str):拼接功能:拼接新的字符串
        System.out.println("concat():"+s.concat("Android")) ;//拼接功能
        System.out.println(s+"IOS");//拼接符号

        System.out.println("---------------------------------------") ;
        // public int indexOf(int ch):返回指定字符第一次出现的字符串内的索引
        //参数为字符/或int: int的值也可以找ASCII码表的字符
        System.out.println("indexOf():"+s.indexOf('l'));
        //int lastIndexOf(int ch)  :查询指定字符最后一次出现的索引值
        System.out.println("lastIndexOf():"+s.lastIndexOf('l'));

        System.out.println("---------------------------------------") ;

        // public String[] split(String regex):字符串的拆分方法:返回的字符串数组 (经常用到)
        //参数可以是正则表达式或则普通字符串---分割符号
        //创建一个新的字符串对象
        String str = "Java-Php-Go-R-Python-C-C++-C#-Net" ;

        String[] strArray = str.split("-") ;
        for(int x = 0 ; x < strArray.length ; x++){
            System.out.print(strArray[x]+" ");
        }
        System.out.println();
        System.out.println("---------------------------------------") ;


        //截取:
        
        System.out.println("subString():"+s.substring(5));
        System.out.println("subString():"+s.substring(5,10));
    }
}
3.2 String类的转换功能

        public char[] toCharArray():将字符串转换字符数组
        byte[] getBytes()  :使用平台的默认字符集进行编码过程:将字符串--->字节数组
        byte[] getBytes(String charset):使用指定的字符集进行编码  "GBK/UTF-8"
        编码和解码
                编码:将能看懂的字符串---->看不懂的字节
                解码:将看不懂的字节----->能看懂的字符串
        public String toLowerCase():将字符串中的每一个字符转换成小写
        public String toUpperCase():将字符串中的每一个字符转换成大写
        static String valueOf(boolean b/float/long/double/int/char /....Object)
        万能方法:
                将任意的数据类型转换成String :静态的功能

public class StringDemo2 {
    public static void main(String[] args) {

        //有一个字符串
        String s = "helloworld" ;

        //public char[] toCharArray():将字符串转换字符数组
        char[] chs = s.toCharArray();
        for (int i = 0; i < chs.length; i++) {
            System.out.println(chs[i]);
        }
        System.out.println("---------------------------------------------") ;
        // byte[] getBytes()  :使用平台的默认字符集进行编码过程:将字符串--->字节数组
        byte[] bytes = s.getBytes() ;
       // System.out.println(bytes);
        //看到字节数组的内容: 使用数组工具Arrays
        //static String toString(任何类型的数组)  :静态功能---将任何类型的数组转换成字符串形式
        System.out.println(Arrays.toString(bytes));
        //[104, 101, 108, 108, 111, 119, 111, 114, 108, 100] :英文:寻找它的ASCII码表
        System.out.println("---------------------------------------------") ;

        //public String toLowerCase():将字符串中的每一个字符转换成小写
        //public String toUpperCase():将字符串中的每一个字符转换成大写
        System.out.println(s.toLowerCase());
        System.out.println(s.toUpperCase());
        System.out.println("---------------------------------------------") ;
        
        int i = 100 ;//int
        String result = String.valueOf(i);
        System.out.println(result) ;//数字字符串 "100"
    }
}
3.2 String类的判断功能

        public boolean contains(String s):判断是否包含子字符串
        public boolean startsWith(String prefix):判断是以指定的字符串开头
        public boolean endsWith(String suffix):判断是是以指定的字符串结尾
        boolean equals(Object anObject)  :判断字符串内容是相等:区分大小写
        boolean equalsIgnoreCase(String anotherString)  :判断字符串内容是否相等,忽略大小写
        public boolean isEmpty():判断字符串是否为空

public class StringDemo3 {
    public static void main(String[] args) {

        //String字符串
        String s = "helloJavaee" ;
//      public boolean contains(String s):判断是否包含子字符串
        System.out.println("contains():"+s.contains("hel"));
        System.out.println("contains():"+s.contains("ak47"));
        //  public boolean startsWith(String prefix):判断是以指定的字符串开头
        //  public boolean endsWith(String suffix):判断是是以指定的字符串结尾
        System.out.println("-----------------------------------------------") ;
        System.out.println(s.startsWith("hel"));
        System.out.println(s.startsWith("张三丰"));
        System.out.println(s.endsWith("ee"));
        System.out.println(s.endsWith("wor"));
        System.out.println("-----------------------------------------------") ;

        String s2 = "helloJavaEE" ;
        System.out.println(s.equals(s2)) ; //区分大小写
        //boolean equalsIgnoreCase(String anotherString)
        System.out.println(s.equalsIgnoreCase(s2));//不区分大小写
        s = "" ;
        System.out.println(s.isEmpty());

    }
}
3.3 String类的其他功能

        public String replace(char oldChar,char newChar):将新的字符值把旧的字符替换掉
        public String replace(String oldChar,String newChar):将新的字符串值把旧的字符串替换掉
        public String replaceAll(String regex,String replacement):使用指定的replacement替换的字符串内容.将符号正则表达式的匹配的字符串替换掉 (用到正则表达式使用)
        参数1:正则表达式   举例: [0-9]
        public String trim():去除字符串前后两端空格.一般用在:IO中传输文件(读写文件)

public class StringDemo {
    public static void main(String[] args) {

        //public String replace(char oldChar,char newChar):将新的字符值把旧的字符串替换掉,返回的新的自测
        String s = "helloworld" ;

        System.out.println("replace():"+s.replace('l','k')) ;
        System.out.println("replace():"+s.replace("world","高圆圆"));

        System.out.println("--------------------------------------------------") ;
        //public String trim():去除字符串前后两端空格 (IO流中,上传文件:保证读取文件内容,先去除两端空格)
        String s2 = " javaEE    " ;
        System.out.println("s2:"+"-----"+s2+"-----");
        System.out.println("-------------------------------------") ;
        System.out.println("s2:"+"-----"+s2.trim()+"-----");
    }
}
4.StringBuffer

        简称"字符串缓冲",线程安全的,而且可变的字符序列!
        StringBuffer和StringBuilder有什么区别?
                共同点:两者都是字符串区,支持可变的字符序列,而且都有互相兼容的API(功能相同的)
                不同点:
                前者:线程安全的类,多线程环境下使用居多-----同步(安全)----->执行效率低
                后者:线程不安全的类,单线程程序中使用居多---->不同步(不安全)----->执行效率高
        构造方法
                StringBuffer();无参构造方法  :使用居多
                StringBuffer(int capacity):指定容量大小的字符串缓冲区
                StringBuffer(String str) :指定缓冲区的具体内容str,容量大小=16个默认容量+当前字符串长度
                public int length()获取字符缓冲区中的长度
                public int capacity():获取字符串缓冲区中的容量大小
 

public class StringBufferDemo{

    public static void main(String[] args) {
        // StringBuffer();无参构造方法
        //创建一个字符串缓冲区对象
        StringBuffer sb = new StringBuffer() ; //默认的初始化容量16个(足够用了)
        System.out.println("sb:"+sb) ;
        System.out.println(sb.length()) ;
        System.out.println(sb.capacity()) ;
        System.out.println("-------------------------") ;

        //  StringBuffer(int capacity):指定容量大小的字符串缓冲区
        StringBuffer sb2 = new StringBuffer(50) ;
        System.out.println("sb2:"+sb2);
        System.out.println(sb2.length());
        System.out.println(sb2.capacity());

        System.out.println("-------------------------") ;

       // StringBuffer(String str) :
        String s = "helloworld" ;
        StringBuffer sb3 = new StringBuffer(s) ;
        System.out.println("sb3:"+sb3);
        System.out.println(sb3.length() );
        System.out.println(sb3.capacity() );

    }
}

        添加字符序列
                StringBuffer append(任何数据类型):将任何数据类型的数据追加字符序列中(字符串缓冲区)
                StringBuffer insert(int offset, String str)  :插入元素:在指定位置插入指定的元素
        删除:
                public StringBuffer deleteCharAt(int index):在指定的位置处删除的指定的字符,返回字符串缓冲区本身
                public StringBuffer delete(int start,int end):删除指定的字符从指定位置开始,到end-1处结束

public class StringBufferDemo2 {
    public static void main(String[] args) {
        //创建一个字符串缓冲区对象
        StringBuffer sb = new StringBuffer() ;
        System.out.println("sb:"+sb);
        // StringBuffer append(任何数据类型):将任何数据类型的数据追加字符序列中(字符串缓冲区)
     
        sb.append("hello");
        sb.append("world") ;
        sb.append("javaee") ;
       
        System.out.println("sb:"+sb) ;
        // StringBuffer insert(int offset, String str)
        sb.insert(5,"张三丰") ;
        System.out.println("sb:"+sb);



        //hello高圆圆worldjavaee
        System.out.println("----------------------------------------------------------") ;
//        public StringBuffer deleteCharAt(int index)

        //需求:删除第一个e字符
        System.out.println(sb.deleteCharAt(1));
        //需求:删除第一个l字符
        System.out.println(sb.deleteCharAt(1));
        //public StringBuffer delete(int start,int end)
        System.out.println(sb.delete(3,7));

    }
}

        public StringBuffer reverse():反转功能

public class StringBufferDemo3 {
    public static void main(String[] args) {
        //已知一个字符串:
     

     //键盘录入一个字符串:将字符串反转
        Scanner sc = new Scanner(System.in) ;

        //提示并录入数据
        System.out.println("请您输入一个字符串:") ;
        String line = sc.nextLine() ;

        //调用功能改进---->String
        String result = reverse(line);
        System.out.println(result);
    }
    public static String reverse(String s){
        //s---->StringBuffer
        //分步实现
//        StringBuffer sb = new StringBuffer(s) ;
//        //反转---并且转换String
//        String result = sb.reverse().toString();
//        return  result ;
        //一步走
        return  new StringBuffer(s).reverse().toString() ;
    }

}

        String substring(int start):从指定位置截取到默认结束,返回的新的字符串
        String substring(int start, int end) :从指定位置开始截取,到指定位置结束(包前不包后)

public class StringDemo4 {
    public static void main(String[] args) {

        StringBuffer sb = new StringBuffer() ;
        sb.append("helllo") ;
        sb.append("world") ;
        System.out.println(sb) ;

        //截取
        String resultStr = sb.substring(5);
        System.out.println(resultStr) ; //返回的被截取的字符串内容

        System.out.println("-------------------------------------") ;
        String resultStr2 = sb.substring(5, 9);
        System.out.println(resultStr2);
    }
}
5.Integer

包装一个对象中的原始类型int的值

public class IntegerDemo1 {
    public static void main(String[] args) {

        //定义int变量
        int i = 100 ;
        Integer ii = new Integer(i) ;
        ii += 200 ;

        System.out.println("ii:"+ii) ;

    }
    
}

Integer的构造方法
        Integer(int value):将int类型构造成Integer对象
        Integer(String s) throws NumberFormatException :将String类型构造成Integer对象
                前提条件:要使用第二个构造方法,前提是必须是数字字符串

public class IntegerDemo2 {
    public static void main(String[] args) {

        //有一个int类型
        int i = 100 ;
        // Integer(int value):将int类型构造成Integer对象
        Integer ii = new Integer(i) ;
        System.out.println(ii); //100

        System.out.println("-------------------------") ;

        //String s = "hello100" ;
        String s = "200" ;

        //Integer(String s) throws NumberFormatException
        Integer i2 = new Integer(s) ;
        System.out.println("i2:"+i2) ;//200
    }
}

        public static String toBinaryString(int i):将整数转换字符串形式的二进制
        public static String toOctalString(int i):将整数转换字符串形式的八进制
        public static String toHexString(int i):将整数转换成字符串形式的十六进制

public class IntegerDemo2 {
    public static void main(String[] args) {

        //求出一个数据的十进制---->二进制  八进制 十六进制
        System.out.println(Integer.toBinaryString(100)) ;
        System.out.println(Integer.toOctalString(100)) ;
        System.out.println(Integer.toHexString(100)) ;

        //求出int类型的取值范围
        //静态的常量值
        //public static final int MAX_VALUE
        //public static final int MIN_VALUE
        System.out.println(Integer.MIN_VALUE) ;
        System.out.println(Integer.MAX_VALUE) ;

    }
}

        int如何和String之间进行相互转换

public class IntegerTest {
    public static void main(String[] args) {

        //int----->String
        //方式1:空串拼接
        int i = 100 ;
        String result = ""+i ;
        System.out.println("result:"+result);//"100"
        System.out.println("-----------------------------") ;

        //方式2:利用中间桥梁:包装类类型Integer
        //Integer(int i)+成员方法String toString()
        Integer ii = new Integer(i) ;
        String result2 = ii.toString();//底层调用---->public static String toString(int i) {}
        System.out.println("result2:"+result2) ; //"100"


        System.out.println("---------------------------------");
        //方式3:public static String toString(int i){}
        String result3 = Integer.toHexString(i);
        System.out.println("result3:"+result3) ; //"100"

        System.out.println("---------------------------------------------------------") ;

        //String----->int
        String s = "50" ;
        //通过Integer的构造方法 Integer(String s) + public int intValue()
        Integer i1 = new Integer(s) ;
        int intResult = i1.intValue();
        System.out.println("intResult:"+intResult);//50
        System.out.println("-----------------------------") ;
        //方式2:Integer类的静态功能:直接转换
        //public static int parseInt(String s)throws NumberFormatException
        //String---long public static long parseLong(String s) throws NumberFormatException
        //其他的String----基本类型:都是通用的方法
        int intResult2 = Integer.parseInt(s) ;
        System.out.println("intResult2:"+intResult2) ; //50


    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/348974.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号