equal方法的作用
若是一个类Student创造两个对象c1和c2
Student c1 = new Student(“zhanghong”,18);
Student c2 = new Student(“zhanghong”,18);
其中属性name,age完全相同,此时
System.out.print(c1 == c2);
结果是false;因为当执行c1=c2时比较的是他们的引用方法
equal相当于上述方法
同时两个对象不能是null
x.equal(x)==true;
x.equal(y)==那么y.equal(x)==true;
x.equal(null)==false
他只能判断出两个引用是不是指向同一个对象;
想要比较内容就重写
例如:String,Date
String s1 = new String(“hello”);
String s2 = new String(“hello”);
System.out.println(s1.equal(s2));
运行结果为true
##此博客仅供本人学习java回头复习使用