This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

equals method in Java

 
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.
 
author
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
 
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.
 
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.
 
Marshal
Posts: 65019
246
  • 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.
 
Ranch Hand
Posts: 63
Spring Java Google App Engine
  • 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.
 
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.
 
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
Boost this thread!