aspose file tools*
The moose likes Beginning Java and the fly likes equals method in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "equals method in Java" Watch "equals method in Java" New topic
Author

equals method in Java

vinay kalal
Greenhorn

Joined: Sep 19, 2010
Posts: 9
Hello
I have a simple confusion.When we use equals() method in Java it means we gonna compare the actual value of that object.But i am not getting correct output in my cases.Does it mean equals() method is use to compare the value of instance variable of classes because instance variables are the state of object.So totally confuse.I have an code below which is giving false output.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

vinay kalal wrote:When we use equals() method in Java it means we gonna compare the actual value of that object


Correct, but it actually have to be implemented -- String, Integer, Double, etc. all implements / override the equals() method to correctly compare the values.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 561
Try printing e and f to see what is being compared with equals() to better understand the result you're getting now.


Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Also read How to Write an Equality Method in Java for more information.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Lester Burnham wrote:Also read How to Write an Equality Method in Java for more information.
That's a good article; two other useful references are Effective Java by Joshua Bloch (you might find a sample chapter of the 1st edition on the web), and Angelika Langer's pages: Google for Angelika Langer Java equals hashCode.
rk sharma
Ranch Hand

Joined: Jun 25, 2011
Posts: 50

It will give "false" as output obviously. because .the default equals() method always compares the references and not the contents. we have to overrride the equals() method as per our requirement.


Regards,

RK
venkatesh Nunna
Greenhorn

Joined: Jul 19, 2011
Posts: 3
public class test {
public static void main(String args[]) {
String s1 = "abc";
String s2 = new String("abc");
if(s1.equals(s2))
System.out.println(3);
else
System.out.println(4);
}
}

If i run the above code, it returns 3. I think object s1 and s2 references are different , but why it returns true.
Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
venkatesh Nunna wrote:public class test {
public static void main(String args[]) {
String s1 = "abc";
String s2 = new String("abc");
if(s1.equals(s2))
System.out.println(3);
else
System.out.println(4);
}
}

If i run the above code, it returns 3. I think object s1 and s2 references are different , but why it returns true.


Because the String class overrides the equals method to compare the string value instead of the references
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: equals method in Java