aspose file tools*
The moose likes Beginning Java and the fly likes about == and equals() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "about == and equals()" Watch "about == and equals()" New topic
Author

about == and equals()

sharma sachin
Greenhorn

Joined: Nov 13, 2009
Posts: 24
Hello javaranch team.
i want to clear my about == and equals()
as far as i knows that == basically compares the two object references
and equals() method checks whether the two object references refers to the same object or not
now i m giving the some sample code but i m not understanding it as per the definition of == and equals()
please remove my confusion



when i execute the above program the output is False however the two object references i.e obj1 and obj3 refers to the same object
i.e Hello
but if i append the statement as obj1=obj3 then the if statement is true
please explain the above statement that why it is happening
Istvan Kovacs
Ranch Hand

Joined: May 06, 2010
Posts: 100
You must override equals() to get meaningful comparisons; by default, the one inherited from Object compares references, just like == does. Note that you should also override hashCode along with equals, and if you implement Comparable, you should make sure that if equals() returns true, compareTo returns 0 - there are only a few special cases when this rule must be broken.

Eclipse can generate equals and hashCode for you.
Read
http://www.ibm.com/developerworks/java/library/j-jtp05273.html
Vineet Kakati
Greenhorn

Joined: May 03, 2010
Posts: 21
Refer the following link to get difference between'==' and equals();
difference
Istvan Kovacs
Ranch Hand

Joined: May 06, 2010
Posts: 100
Vineet Kakati wrote:Refer the following link to get difference between'==' and equals();
difference


Be careful with that article! It might confuse people, as it does not explain why the following is true


You will get TRUE for both the tests.


For an explanation, see my post at http://www.coderanch.com/t/487350/Performance/java/Best-way-compare-strings (it's one of the last posts there).

I think a good source of info is the javadoc for java.lang.Object - check equals() and hashCode, as well as the developerWorks article.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40071
    
  28
sharma sachin wrote: . . . equals() method checks whether the two object references refers to the same object or not . . .
No, that is only the un-overridden Object#equals() method. You override it to accept other types of equality. Google for Angelika Langer Java equals or find the chapter in Joshua Bloch's Effective Java, for more details. It looks easy at first sight, but it is actually very difficult.
Bimal Sahay
Greenhorn

Joined: Aug 14, 2009
Posts: 4
Hi

Firstly, your Obj1 and Obj3 are not refering to the same object. Whenever you do a new it will create a seperate object in memory.

I tried you code with both equals and == but it gave false both time. This is what is expected.

For Object class, since the equals and Hashcode methods are not overriden, it checks for object reference. For Object both equals and == mean same.

Now, the reason why people would ask you to override equals and hashcode is to define you own way of distinguishing between two objects. As a simple example, if you have 2 Objects of Dog class, you need to define why these two Dogs are different. What rule Java should use to distinguish between them.

For String class, the equals and Hashcode are overriden.

Try below code and see the difference(I have used object of class String):

class B
{
public static void main(String ar[])
{
Object obj1=new String("Hello");
Object obj2=new String("Hi");
Object obj3=new String("Hello");

if(obj1.equals(obj3))
//if(obj1 == obj3)
System.out.println("True");
else
System.out.println("False");

}
}


Bimal
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about == and equals()