wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes equals VS '==' on primitives and Boxed variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "equals VS Watch "equals VS New topic
Author

equals VS '==' on primitives and Boxed variables

Ayesha Farid
Greenhorn

Joined: Oct 26, 2012
Posts: 3
please help with my doubts:--

Integer io=new Integer("23");
int i=23;
System.out.println(io.equals(i)); // true ...why? ... but other way round i.equals(io) gives error (i cannot be deprecated...no idea whats dat )
System.out.println(5.00f==5L); // long and float are incompatible types still no error ...why?

Long lt=53L;
Integer it=53;
int p=53;
System.out.println(lt==p); //true why the error incomparable dun pop-up ...becoz other way round the next statement execute with error...
System.out.println(lt==it); //error (Incomparable types) why
System.out.println(lt.equals(it)); //true becoz they refer to different objects...IS this the reason??

thank You..
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Ayesha Farid wrote:Integer io=new Integer("23");
int i=23;
System.out.println(io.equals(i)); // true ...why? ... but other way round i.equals(io) gives error (i cannot be deprecated...no idea whats dat )

1. Because the value of the Integer object that io refers to has the value 23.
2. Because you cannot call a method such as equals() on a primitive type; note that i is of the primitive type int.

Ayesha Farid wrote:
System.out.println(5.00f==5L); // long and float are incompatible types still no error ...why?

Because the long value 5L is automatically converted to float using a widening primitive conversion.

Ayesha Farid wrote:
Long lt=53L;
Integer it=53;
int p=53;
System.out.println(lt==p); //true why the error incomparable dun pop-up ...becoz other way round the next statement execute with error...

The variable lt is auto-unboxed to a long; the value of the variable p is converted from int to long by a widening primitive conversion, and then the values can be compared. I don't know what you mean by "other way round the next statement execute with error". If you write p==lt instead of lt==p it still works in the same way.

Ayesha Farid wrote:
System.out.println(lt==it); //error (Incomparable types) why

Because lt is a Long object and it is an Integer object, and Long and Integer are two classes that are unrelated to each other (one cannot be automatically converted to the other).

Ayesha Farid wrote:
System.out.println(lt.equals(it)); //true becoz they refer to different objects...IS this the reason??

No, the result is false, and not because lt and it are different objects, but because they are instances of different classes. If you pass anything other than a Long object to the equals() method of class Long, it will return false, and so because it is not a Long, the result is false.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: equals VS '==' on primitives and Boxed variables