referenceequals

why equals() method when we have == operator? [duplicate]

五迷三道 提交于 2019-11-26 05:15:53
This question already has an answer here: How do I compare strings in Java? 23 answers When i see the implementation of equals() method it does nothing but same as what == does. So my question is what was the need to have this as separate method when we have == operator which does the same work? You can not overload the == operator, but you can override equals(Object) if you want it to behave differently from the == operator, i.e. not compare references but actually compare the objects (e.g. using all or some of their fields). Also, if you do override equals(Object) , have a look at hashCode()

why equals() method when we have == operator? [duplicate]

一笑奈何 提交于 2019-11-26 01:54:18
问题 This question already has answers here : How do I compare strings in Java? (23 answers) Closed 6 years ago . When i see the implementation of equals() method it does nothing but same as what == does. So my question is what was the need to have this as separate method when we have == operator which does the same work? 回答1: You can not overload the == operator, but you can override equals(Object) if you want it to behave differently from the == operator, i.e. not compare references but actually

Difference between null and empty (“”) Java String

匆匆过客 提交于 2019-11-25 23:51:29
问题 What is the difference between null and the \"\" (empty string)? I have written some simple code: String a = \"\"; String b = null; System.out.println(a == b); // false System.out.println(a.equals(b)); // false Both statements return false . It seems, I am not able to find what is the actual difference between them. 回答1: "" is an actual string, albeit an empty one. null, however, means that the String variable points to nothing. a==b returns false because "" and null do not occupy the same