Object 方法

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));
    }
}

  • 12
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值