Object toString方法的作用是返回对象的字符串形式,
存在的意义是让子类重写, 以便返回子类对象的的内容。
Object equals方法的作用是
默认是比较2个对象的地址是否相同
存在的意义是 让子类重写,以便于比较2个对象的内容是否相同。
clone()
当某个对象使用该方法时,该方法会复制一个一模一样的新对象返回。
浅拷贝
拷贝出的新对象与原对象的数据一模一样( 引用类型拷贝的只是地址)
深拷贝
对象中基本类型的数据直接拷贝;
对象中字符串的数据拷贝的还是地址;
对象中还包含的其他对象,不会拷贝地址 , 会创建新对象;
public class Test {
public static void main(String[] args) throws CloneNotSupportedException {
Student student = new Student(" 赵敏", 23, new double[] {1,2,3});
Student student1 = new Student("赵敏", 23, new double[] {1,2,3});
System.out.println(student);
System.out.println(student.equals(null));
System.out.println(student.equals(student1));
System.out.println(student == student1);
Student student2 = (Student) student.clone();
System.out.println(student.getName());
System.out.println(student.getScores());
System.out.println(student.getAge());
System.out.println(student2.getName());
System.out.println(student2.getScores());
System.out.println(student2.getAge());
}
}
public class TestObjects {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "hello";
System.out.println(s1.equals(s2));
System.out.println(Objects.equals(s2, s1)); //更安全
System.out.println(Objects.isNull(s1));
}
}