Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals method in Java

 
vinay kalal
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21208
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Greg Brannon
Bartender
Posts: 563
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try printing e and f to see what is being compared with equals() to better understand the result you're getting now.
 
Lester Burnham
Rancher
Posts: 1337
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also read How to Write an Equality Method in Java for more information.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Radhakrishna Sharma Gorenta
Ranch Hand
Posts: 63
Google App Engine Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
venkatesh Nunna
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 164
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic