【发布时间】:2016-08-04 04:53:23
【问题描述】:
public class Employee {
String name;
public Employee(String var){ this.name=var;}
}
public class Main {
public static void main(String[] args){
String s1=new String("joe");
String s2=new String("joe");
Employee e1=new Employee("joe");
Employee e2=new Employee("joe");
system.out.println("When comparing String obj:"+s1.equals(s2)); output:TRUE
system.out.println("When comparing Employee obj:+e1.equals(e2)): output:FALSE
我知道我们必须重写 Employee 类,但为什么它适用于 String 类,我也无法在 Oracle 文档中找到 String 类的 equals 重写方法。请需要帮助!!
【问题讨论】:
-
当您声明单独的对象时,如果您将第二个对象声明为 Employee e2 = e1;然后它将返回 true
标签: java string object equals hashcode