Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Core java Object's equals() method comparing with ==operator

 
Deepak Bobal
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am confused taking the difference between equals and == operator.How and where the work?And one more thing,what does it stand foe equating two object references ?
if Integer a=2;
and Integer b=3;
and if i do a=b;
so according to me bit pattern of b is copied to a .But when i see output i get value of b is transformed to a.And giving True while using equals() and ==.
I m bit confused.
please help me out.
Thanks n regards
Deepak
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Deepak", please check your private messages for an important administrative matter.
 
Jorge Pinho
Greenhorn
Posts: 25
MySQL Database PHP Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Deepak,

Here it goes...

The == gives true if the Objects are the same, so...

String a = "a";
String b = a;

a == b <- true

String c = "c";
String d = "c";

c == d <- false

the equals() method is a method that every class object has, so that the developer can set some logic to tell if the objects have some equality.

thats why:

String a = "a";
String b = "a";

a.equals(b) <- true
but
a == b <- false (diferent objects)

hope the helps, dont forget to see the manual, and have fun writing code
 
Campbell Ritchie
Sheriff
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it doesn't. You have forgotten that Strings are immutable, so some of those examples you gave won't work as you suggested.

And Deepak, you haven't taken notice of what Bear Bibeault told you.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jorge Pinho:

String a = "a";
String b = "a";

a.equals(b) <- true
but
a == b <- false (diferent objects)


Immutability is not the reason that this example is wrong.
In the quoted example a == b will actually return true because String literals are held in a pool and there is only one instance of each literal. Therefore a and b are both referencing the same object and so are equal.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right; I wasn't clear about why those examples wouldn't work as they thought.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The == operator is a simple comparison of values. For object references, the values are the references, so x == y returns true if x and y reference the same object.

The equals method can compare two different objects for equality. But here, "equal" is defined by the programmer when they override the method for a specific class. For example, if you are writing a Car class and all you care about is color, then you could implement equals to return true if both cars are the same color. It's probably not the best way to compare cars, but the point is it's up to the programmer to decide what "equals" means.

(As mentioned above, be careful when comparing Strings to each other, and when comparing autoboxed wrapper instances to each other. Under certain situations, these use "pools." See Strings, Literally and JLS - 5.1.7 Boxing Conversion.)
 
Jorge Pinho
Greenhorn
Posts: 25
MySQL Database PHP Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Giving a simple example that works...

Integer i1 = new Integer(5);
Integer i2 = new Integer(5);

i1.equals(i2) <- true (Integer.equals() does the comparing logic)

i1 == i2 <- false (diferent objects)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic