Java常用类库

1、Object类

package LESSON8;
public class DEMO1 {
    /**java.lang.Object类是类层次结构中最顶层父类(根类/基类/超类)。
    所有的类(包括数组)直接或者间接的继承自Object类,同时也继承了该类中的方法。
    Object的构造方法
    Object类只有一个无参的构造方法 
    Object()
    Object中常用的方法
    boolean equals(Object o) 比较两个对象是否相同
    int hashCode() 返回一个对象的哈希码
    String toString() 将一个对象转换为字符串
     */
    public static void main(String[] args) {
        Object o1=new Object();
        Object o2=new Object();
        Object o3=o2;
        //equals(Object object)方法和==比较的区别
        //==可以比较基本数据类型也可以比较引用数据类型,equals只能用来比较引用数据类型
        //在Object类中。==和equals比较的是首地址和内容是否相等
        //在其他类中,==表示比较首地址和内容是否相等,而equals只比较内容
        System.out.println(o1.equals(o2));//false
        System.out.println(o1==o2);//false
        System.out.println(o2.equals(o3));//true
        //获取对象的哈希码
        System.out.println(o1.hashCode());//24417480
        System.out.println(o2.hashCode());//13884241
        System.out.println(o3.hashCode());//13884241
        o1=o1.toString();
        if(o1 instanceof String){
            System.out.println("我属于String");
        }

    }

}
package ceshi;
public class ceshi2 {        
    public static void main(String[] args) {
        ceshi2 ceshi1=new ceshi2();
        ceshi2 ceshi2=new ceshi2();
        System.out.println(ceshi1==ceshi2);//false   首地址不同  
        System.out.println(ceshi1.equals(ceshi2));//false  自己写的测试类,默认继承Object类,equals为Object类的方法。在object类中,==和equals比较的是首地址和内容是否相同
        
        System.out.println("------------------------");
        String a=new String("foo");
        String b=new String("foo");
        System.out.println(a==b);//false   //在其他类中(如String),==表示比较首地址和内容是否相等,而equals只比较内容
        System.out.println(a.equals(b));//true                
    }
}
package ceshi;
public class ceshi3 extends ceshi2{//继承ceshi2但ceshi2继承Object
    public static void main(String[] args) {
        ceshi3 ceshi1=new ceshi3();
        ceshi3 ceshi2=new ceshi3();
        System.out.println(ceshi1==ceshi2);//false   
        System.out.println(ceshi1.equals(ceshi2));//false            
    }

}

 

重写Object类的equals方法和toString方法

package LESSON8;
public class Student {
    private String stuNo;
    private String name;
    public void setStuNo(String stuNo) {
        this.stuNo = stuNo;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getStuNo() {
        return stuNo;
    }
    public String getName() {
        return name;
    }
    //
    @Override//重写Object类的equals方法
    public boolean equals(Object arg0) {
        if(this.stuNo.equals(arg0))
        {
            return true;
            }
        else {
            return false;}
        }
    @Override//重写Object类的toString方法,返回指定的字符串内容。实例化后打印该类的对象时会出现这个指定返回的字符串System.out.println(stu);
    public String toString() {
        // TODO Auto-generated method stub
        return "姓名:"+this.name+"学号:"+this.stuNo;
    }
    public static void main(String[] args) {
        Student stu=new Student();
        stu.setName("张三");
        stu.setStuNo("201301");
        System.out.println(stu.equals("201302"));
        System.out.println(stu);
    }

}

2、String类

package LESSON8;
public class DEMO2 {
    /**
     String类代表字符串,是不可变对象,字符串是常量。它的值创建之后就不可以再修改了
       第一种创建方法:String str=new String();
       第二种创建方法:String str="";
声明和创建字符串对象的方式
    第一种方式:使用new调用构造器
        String s=new   String(..);
    对象保存在堆内存中,每次都会返回一个新的对象。
    第二种方式:直接赋值
        String s="...";
    对象保存在常量池中,如果不存在相同内容的对象将创建新    对象,如果存在将不创建新对象。
*/ public static void main(String[] args) {
     String s15="aaa";
        String s16="aaa";
        System.out.println(s15==s16);//true
        System.out.println(s15.equals(s16));//true
        String s17=new String("aaa");
        String s18=new String("aaa");
        System.out.println(s17==s18);//false
        System.out.println(s17.equals(s18));//true
String s1
="abc"; String s2="acc"; //System.out.println(s1.charAt(0));//返回下标对应的char值 System.out.println(s1.charAt(0));//a //被比较的对象,靠后差几位就返回负几 //被比较的对象,靠前差几位就返回正几 System.out.println(s1.compareTo(s2));//-1 //如果两个字符串长度不相同时,比较的是长度 String s3="baaa"; String s4="a"; String ss="b"; System.out.println(s3.compareTo(s4));//1(a在b前面一位) System.out.println(s3.compareTo(ss));//3 String s5="abcd"; System.out.println(s5.concat("123"));//连接两个字符串 System.out.println(s5+"123");//连接两个字符串 System.out.println(s5.contains("abc"));//是否包含某个字符 System.out.println(s5.endsWith("acd"));//是否以某个字符串结束 System.out.println(s5.startsWith("a"));//是否以某个字符串开始 System.out.println(s5.equals("abcd"));//比较内容是否相同 System.out.println("------------------------"); byte[] bs=s5.getBytes();//返回一个byte类型的数组 for (int i = 0; i < bs.length; i++) { System.out.println(bs[i]);//97--- } System.out.println("--------------------------"); String s6="abada"; System.out.println(s6.indexOf("a"));//字符串中字符第一次出现的索引 System.out.println(s6.lastIndexOf("a"));//字符串中字符最后一次出现的索引 String s7="abcd"; System.out.println(s7.isEmpty());//是否为空,长度是否为0 System.out.println(s7.length());//返回字符串的长度 String s8="你最喜欢读书"; System.out.println(s8.replace("读书", "打游戏"));//替换字符串中的字符 System.out.println("----------------------------"); String s9="1999-12-3"; String [] str=s9.split("-");//指定字符串进行拆分,返回一个字符串数组 for (String s : str) { System.out.print(s); } System.out.println("--------------------------------"); String s10="Abcde"; System.out.println(s10.substring(1));//截取字符串,包括该下标对应字符串 bcde System.out.println(s10.substring(1,3));//截取字符串,包前不包后 bc System.out.println(s10.toUpperCase());//转大写 System.out.println(s10.toLowerCase());//转小写 System.out.println("-----------------------------------"); String s11="abcde"; char[] c=s11.toCharArray();//返回一个字符数组 for (char ch : c) { System.out.println(ch); } System.out.println("-------------------------------------"); String s12=" ab cd "; System.out.println(s12.trim());//去掉前后空格 //String.valueOf(基本数据类型);//将基本数据类型转化为String类型 int password=123456; System.out.println(String.valueOf(password) instanceof String); } }

3、StringBuffer和StringBuilder

  都是带有缓冲区的可变字符串,StringBuffer是线程安全的、StringBuilder线程不安全

package LESSON8;
public class DEMO3 {
    /**
    一个String对象的长度是固定的,不能改变它的内容,也不能附加新的字符至String对象中。
    可以使用加号“+”运算符来连接字符串以达到附加新字符或字符串的目的,但使用加号“+”运算符
    会产生一个新的String实例,即需要另外分配空间。如果既想节省开销,又能改变字符串的内容,
    则可以使用StringBuilder    类。
     */
    public static void main(String[] args) {        
        StringBuffer sf1=new StringBuffer("可变字符串");
        System.out.println(sf1);
        sf1.append("线程安全");//追加        
        System.out.println(sf1);//输出“可变字符串线程安全”
        
        StringBuffer sf2=new StringBuffer("可变字符串");
        sf2.insert(0, "线性安全的");//在指定下标之前插入内容        
        System.out.println(sf2);//输出“线性安全的可变字符串”   
//注意:s1.insert(1, s1);//不在0位置插入自己时会出现错误 方法bug

StringBuffer sf3=new StringBuffer("可变字符串"); sf3.reverse();//反转该字符串的前后顺序 System.out.println(sf3);//输出“ 串符字变可” String s1=new String("不可变字符串"); System.out.println(s1);//输出“不可变字符串” s1.concat("啊"); System.out.println(s1);//输出“不可变字符串” } }

4、包装类

八种基本数据类型的包装类Byte、Short、Integer、Long、Double、Float、Boolean、Character。

包装类提供了字符串、基本数据类型和包装类相互转化的方法。

package LESSON8;
public class DEMO4 {
    /**
    以int为例*/
    public static void main(String[] args) {
        //int >>Integer
        int in1=123;
        Integer integer1=in1;//1装箱
        System.out.println(integer1);
        
        Integer integer2=new Integer(in1);//2构造器
        System.out.println(in1);
        
        Integer integer3=Integer.valueOf(in1);//3调用Integer类的valueOf方法
        System.out.println(integer3);
        
        //Integer>>int
        Integer integer4=new Integer(1234);
        int in2=integer4;//1拆箱
        System.out.println(in2);        
        int in3=integer4.intValue();//2调用Integer类的intValue方法
        System.out.println(in3);
    
        
        //int>>String
        
        int in4=12345;
        String str1=String.valueOf(in4);//1调用String类的valueOf方法
        System.out.println(str1);
        
        Integer integer5=new Integer(in4);
        String str2=integer5.toString();//2调用Integer类的toString方法
        System.out.println(str2);
        
        String str3=in4+" ";//3 
        System.out.println(str3);
        
        //String>>int
        String str4="123456";
        int in5=Integer.parseInt(str4);//1调用Integer类的parseInt方法
        System.out.println(in5);
        int in6=Integer.valueOf(str4);//2调用Integer类的valueOf方法转化为Integer类型,然后拆箱
        System.out.println(in6);
        
         //String>>Integer
        String str5="1234567";
        Integer integer6=Integer.valueOf(str5);//1调用Integer类的valueOf方法
        System.out.println(integer6);        
        Integer integer7=new Integer(str5);//2构造器
        System.out.println(integer7);
        
        
         //Integer>>String
        Integer integer8=new Integer(12345678);
        String str6=integer8.toString();//1调用Integer类的toString方法
        System.out.println(str6);
        
        String str7=String.valueOf(integer8);//2调用String类的valueOf方法
        System.out.println(str7);
        
    }

}

Character包装类除了提供以上char和Character相互转换的方法外也提供了以下有用的方法

package LESSON8;
public class DEMO5 {    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(Character.isLetter('A'));//是否是字母
        System.out.println(Character.isDigit('1'));//是否是数字
        System.out.println(Character.isUpperCase('A'));//
        System.out.println(Character.isLowerCase('a'));
        System.out.println(Character.toUpperCase('b'));
        System.out.println(Character.toLowerCase('C'));
        System.out.println(Character.toString('f') instanceof String);
        System.out.println(Character.isWhitespace('a'));//是否是空格

    }

}

5、Math类

    在java.lang.Math类中提供了在数学应用中常见的常量(如:PI值)以及方法(如:三角函数)

package LESSON8;
public class DEMO6 {    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(Math.PI);
        System.out.println(Math.abs(-11));
        System.out.println(Math.ceil(12.3));//向上取整
        System.out.println(Math.floor(12.3));//向下取整
        System.out.println(Math.max(3, 5));
        System.out.println(Math.min(7, 5));        
        System.out.println(Math.round(-11.5));//结果为-11 四舍五入 算法为:Math.floor(x+0.5)。即为负数时最后一位为5时会出现错误
        System.out.println(Math.pow(2, 3));
        System.out.println(Math.random());//随机数,0-1(包括0不包括1)
        System.out.println(Math.sqrt(16));
    }
}

6、System类
    static void exit(int status)  终止当前正在运行的 Java 虚拟机。
    static void gc()     运行垃圾回收器。

package LESSON8;
public class DEMO7 {
    public static void main(String[] args) {
        if (2>11) {
            System.exit(0);//退出JVM
            
        }
        System.out.println("虚拟机");
        System.gc();//运行垃圾回收站

    }

}

 

 






posted @ 2019-06-04 21:12  勤奋的园  阅读(1330)  评论(0编辑  收藏  举报