javaSE21/9/12

常用类

Object类

  • 超类,位于继承树的最顶层

getClass()方法

  • public final Class getClass(){} 返回为引用中存储的实际对象,判断两个对象是不是同一个class
public class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
public class Test {
    public static void main(String[] args) {
        Student s1 = new Student("张三",20);//有参构造
        Student s2 = new Student("李四",21);
        //判断s1和s2是不是同一个类型,这时就可以用getClass方法
        Class class1 = s1.getClass();//
        Class class2 = s2.getClass();
        if (class1==class2){
            System.out.println("s1和s2属于同一个类型");
        }else{
            System.out.println("s1和s2不属于同一个类型");
        }
    }

}

hashCode()方法

  • public int hashCode(){} 返回该对象的哈希码值
  • 一般相同对象返回相同的哈希码值,可以判断是不是同一个对象
public class Test {
    public static void main(String[] args) {
        Student s1 = new Student("张三",20);//有参构造
        Student s2 = new Student("李四",21);
        System.out.println(s1.hashCode());//输出哈希码值
        System.out.println(s2.hashCode());
    }

}

toString()方法

  • public String toString(){} 返回该对象的字符串表示
  • 可以根据程序需求覆盖该方法
public String toString(){//方法重写
        return name+":"+age;
    }
public class Test {
    public static void main(String[] args) {
        Student s1 = new Student("张三",20);//有参构造
        Student s2 = new Student("李四",21);
        System.out.println(s1.toString());//打印该类的全名称和对象的哈希值
        System.out.println(s2.toString());//对toString方法重写后,输出重写后的方法
    }

}

equals()方法

  • public boolean equals(Object obj){} 比较两个对象是否相等
  • 默认实现为(this==obj),比较两个对象地址是否相同
  • 可进行覆盖,比较两个对象的内容是否相同
  • equals()方法覆盖步骤
    比较两个引用是否指向同样一个对象
    判断obj是否为null
    判断两个引用指向的实际对象类型是否一致
    强制类型转换
    依次比较各个属性值是否相同
public boolean equals(Object obj) {//equals方法重写
        if (this==obj){
            return true;
        }
        if (obj==null){
            return false;
        }
        if (this.equals(obj)){
            return true;
        }
        if (obj instanceof Student){
            Student s = (Student)obj;
            if (this.name.equals(s.getName())&&this.age==s.getAge()){
                return true;
            }
        }
        return false;
    }

finalize()方法

  • 当对象被判定为垃圾对象时,有JVM自动调用此方法,用以标记垃圾对象,进入回收队列
  • 自动回收机制
  • 手动回收机制:使用System.gc();通知JVM执行垃圾回收

包装类

  • 基本数据类型所对应的引用数据类型叫包装类
  • Object可统一所有数据,包装类的默认值是null
  • 基本类型对应的包装类型
    byte Byte
    short Short
    int Integer
    long Long
    float Float
    double Double
    boolean Boolean
    char Character

类型转换与装箱,拆箱

  • 基本类型转换为引用类型的过程叫装箱,反之叫拆箱
  • JDK1.5之前
public class Test {
    public static void main(String[] args) {
        //装箱:基本类型转换为引用类型
        int num1= 10;//基本类型存放在栈空间
        //使用Integer包装类创建对象
        Integer n1 = new Integer(num1);//基本类型转换成包装类
        Integer n2 = Integer.valueOf(num1);//第二种装箱的方法
        //拆箱:引用类型转为基本类型
         Integer n3 = new Integer(100);//引用类型在堆空间
         int num2 = n3.intValue();//引用类型转数据类型

    }

}
  • 自动装箱拆箱
public class Test {
    public static void main(String[] args) {
        //自动装箱
        int age = 20;
        Integer integer = age;
        //自动拆箱
        int integer2 = integer;
    }

}

parse基本类型()静态方法

  • 可以实现字符串和基本类型的转换
public class Test {
    public static void main(String[] args) {
        //基本类型转换为字符串
        int n1 = 20;
        String n2 = Integer.toString(n1);//Integer类中的toString方法
        System.out.println(n1);
        //字符串转换为基本类型
        String str = "134";//只能是字符串类型的数字
        int str2 = Integer.parseInt(str);//使用Integer中的parseInt方法
        System.out.println(str2);
        //boolean的字符串类型转为基本类型,"true"==>true 非"true" ==>false
        String b1 = "true";
        boolean b2 = Boolean.parseBoolean(b1);
        System.out.println(b2);

    }

}
posted @ 2021-09-12 00:01  想吃坚果  阅读(42)  评论(0)    收藏  举报