• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

equals VS '==' on primitives and Boxed variables

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
The City calls upon her steadfast protectors. Now for a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic