• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals versus ==

 
sindura kapur
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suppose

class Dog
Dog d1=new Dog();
Dog d2=new Dog();
boolean b1=(d1==d2);
boolean b2=(d1.equals(d2));System.out.println(b1,b2);

what will be the values of b1 and b2
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both will be false.

== will check if two references point to the same object. For instance, if you take a third variable and assign the value of d1 to it (Dog d3 = d1) , then d1 == d3.

equals will use some developer defined comparison. For Strings it is whether or not both Strings have the exact same characters, for Integers it is whether they represent the same int value, but this comparison can easily include many many fields.

However, you as a developer will have to override equals yourself. If you do not, you will inherit the implementation of your parent class. For Dog, that is Object, and Object simply uses == in its check:

Please read the API of Object.equals and Object.hashCode for the exact rules.
[ July 27, 2008: Message edited by: Rob Prime ]
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Questions on equals() versus == get asked a lot. You can take a look at this recent thread for a detailed discussion on the subject. And if you search the forum, you'll find a number of other discussions on it.
 
Agarwal Priyanka
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
all the primitive type wrapper classes override the Object.equals() method to compare the value of the objects; the default Object.equals() checks if the variables reference the same object.
equals(Object anObject)Compares this string to the specified object



Output

str1 == str2 : true
str2 == str3 : true
str3 == str1 : true
str4 == str5 : false
str1 == str4 : false
str1 == str5 : false
str1.equals(str2) : true
str2.equals(str3) : true
str3.equals(str1) : true
str4.equals(str5) : true
str1.equals(str4) : true
str1.equals(str5) : true
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Understanding equals is an important aspect in Java. Refer to the article here.
 
camilo lopes
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
equals you test the values and == you test reference
 
sindura kapur
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i am compiling this Strings1.java i am getting :reached end of file while parsing??what should i do
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That message means that the compiler was reading your file and was expecting more. It's usually a sign of unbalanced braces. Make sure all your opening braces are closed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic