• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals() vs ==

 
Faraah Dabhoiwala
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Actually i wanted to know the exact difference between equals and ==

as far as i have understood from scjp 5 K & B,,

i have concluded that equals() compares values whereas == compares values as well as object type..

but when i run code it gives unexpected answers everytime....

can anyone please explain me what is the exact meaning of these two??
 
John Bengler
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shadab,

if I want to formulate it in a short statement like your's I would say:

equals() compares values and object type whereas == compares the object instance (the adress in memory).


You can control the result of equals() by overriding your classes hashCode() and equals() methods.


You can find more on this if you search the forum for "equals".



John
 
Faraah Dabhoiwala
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




output




Here it gives true for i==D ,I==d,i==I,d==D and so on....


and the lines in comment shows compile time error....if i remove the comments
 
Akanksha Mittal
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Since Integer and Double override equals method, thats why the uncommented lines work and commented do not.

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because variable d is a double (primitive type), and not a Double (instance of wrapper class).
You can't call methods on primitive types, therefore d.equals(...) does not work.

The same holds ofcourse for i, which is an int (primitive type) and not an Integer (object).
 
Faraah Dabhoiwala
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for solving my second doubt but my 1st doubt was difference between equals () and ==
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you use the == operator on two values, it will check if the two values refer to the exact same object. Note that if you have to different objects that have the same value, == will return false. For example:

When used on variables of a primitive type, == will compare values. For example:

The equals() method is just a normal method, that you can override in your own classes. It's supposed to compare two objects by value - it should return true when the value of the two objects is the same (and false otherwise).
 
Faraah Dabhoiwala
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks...

ya but


when i did System.out.println(i==I);

it returned true why so??

Here i is primitive type and I is an Object of type Integer

so as far as i understood we are not comparing objects hence here it should return false !!
 
Deepak Borania
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shadab Wadiwala wrote:Thanks...

ya but


when i did System.out.println(i==I);

it returned true why so??


I think the value in 'I' is unboxed and is then compared with the primitive 'i' . Hence true.
I can't think of any other reason, why it should return true.

 
Tanmoy Dhara
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why the rules of auto-boxing and auto-unboxing didn't apply to equals method??(code is given in the third post of this topic thread by the starter). When he was saying -
int i=4;
System.out.println(i.equals(4));

why i is not automatically converted to an object of Integer class ?
 
Deepak Borania
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because 'i' is primitive with no methods whatsoever.
However you can box it yourself to get the desired result like this :



JLS conversions
 
Tanmoy Dhara
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


System.out.println (((Integer)i).equals(4)); //prints true


in the JLS link you provided,only boxing conversions are clarified(no mention of autoboxing). So, i still don't understand that why (Integer)i part of the above mentioned statement you wrote, can not be done automatically as far as J2SE 5.0 or later versions are concerned.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic