learn java
The moose likes Beginning Java and the fly likes Core java Object's equals() method comparing with ==operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Core java Object Watch "Core java Object New topic

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

Deepak Bobal
Ranch Hand

Joined: Feb 06, 2008
Posts: 96
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

Constant dripping hollows out a stone....
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 62307

"Deepak", please check your private messages for an important administrative matter.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jorge Pinho

Joined: May 16, 2003
Posts: 25

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
a == b <- false (diferent objects)

hope the helps, dont forget to see the manual, and have fun writing code

Jorge Pinho / SCJP 5
Live Help via Skype - lets.talk.about.help
Dont wait for forum answers
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 41631
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

Joined: Aug 05, 2005
Posts: 3742
Originally posted by Jorge Pinho:

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

a.equals(b) <- true
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

Joined: Oct 13, 2005
Posts: 41631
You are right; I wasn't clear about why those examples wouldn't work as they thought.
marc weber

Joined: Aug 31, 2004
Posts: 11343

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.)

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Jorge Pinho

Joined: May 16, 2003
Posts: 25

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)
Have you tried LearnNowOnline? http://www.learnnowonline.com/
subject: Core java Object's equals() method comparing with ==operator