Question:
What are the differences between == and .equals() in java?
anonymous
1970-01-01 00:00:00 UTC
What are the differences between == and .equals() in java?
Three answers:
Neunerball
2010-06-03 06:24:15 UTC
Good question!



For PRIMITIVE TYPES, the concept or equality is straightforward and is subject to promotion rules. E. g. a FLOAT value of 10.0 is equal to a BYTE 10. But for variables of OBJECT type the "value" is taken as the reference to the object (the memory address). So, e. g. when you compare one String variable to another you use .equals()...
EricJames
2010-06-03 02:34:41 UTC
If you want to see if two references are the same object or to compare enum values use "==".



The equals method is defined for the Object class and compares values rather than references.



here's a good reference: http://leepoint.net/notes-java/data/expressions/22compareobjects.html
?
2010-06-02 19:34:33 UTC
The == operator compares two objects to determine if they are the same object in memory i.e. present in the same memory location. It is possible for two String objects to have the same value, but located in different areas of memory. == compares references while .equals compares contents. The method public boolean equals(Object obj) is provided by the Object class and can be overridden. The default implementation returns true only if the object is compared with itself, which is equivalent to the equality operator == being used to compare aliases to the object. String, BitSet, Date, and File override the equals() method. For two String objects, value equality means that they contain the same character

sequence. For the Wrapper classes, value equality means that the primitive values are equal.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...