假设有个类Student,有两个Student对象
Student s1 = new Student("zhangsan", 18);
Student s2 = new Student("zhangsan", 18);
System.out.println(s1.equals(s2));
Student类里有个方法
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (!(obj instanceof Student))
- return false;
- Student s = (Student) obj;
- return this.name.equals(s.name) && this.age == s.age;
- //return this.name == s.name && this.age == s.age;
复制代码 最后一行注释掉的跟前一行的输出结果是一样的,也就是用==比较的地址值是相等的,那究竟内存中是存在一个Student对象呢,还是两个?
|
|