Using the equals() method with String and Object in Java

后端 未结 7 712
情话喂你
情话喂你 2021-02-02 03:11
Object o1 = new Object();
Object o2 = new Object();
//o1=o2;
System.out.println(o1.equals(o2));

It returns false. It can return true

7条回答
  •  误落风尘
    2021-02-02 03:42

    The equals() Method of the Object class doesn't know how to compare Strings, it only knows how to compare objects. For comparing strings, a string class will override the equals() Method and compare strings in it.

    Object.equals() will compare only references, where String.equals() will compare values.

提交回复
热议问题