aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes equals() vs == Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "equals() vs ==" Watch "equals() vs ==" New topic
Author

equals() vs ==

Faraah Dabhoiwala
Greenhorn

Joined: May 30, 2007
Posts: 21
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??


<b>Please don't visit my homepage...</b><br /> <a href="http://shadabworld.110mb.com" target="_blank" rel="nofollow">http://shadabworld.110mb.com</a>
John Bengler
Ranch Hand

Joined: Feb 12, 2009
Posts: 133
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

Joined: May 30, 2007
Posts: 21




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

Joined: Jul 29, 2009
Posts: 26


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

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14350
    
  22

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


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Faraah Dabhoiwala
Greenhorn

Joined: May 30, 2007
Posts: 21
Thanks for solving my second doubt but my 1st doubt was difference between equals () and ==
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14350
    
  22

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

Joined: May 30, 2007
Posts: 21
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

Joined: Jul 28, 2009
Posts: 45
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

Joined: Aug 06, 2009
Posts: 62
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 ?


SCJP 5
"I fear not the man who has practiced 10,000 kicks once, but I fear the man who has practiced one kick 10,000 times" - Bruce Lee
Deepak Borania
Ranch Hand

Joined: Jul 28, 2009
Posts: 45
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

Joined: Aug 06, 2009
Posts: 62


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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: equals() vs ==