• Post Reply Bookmark Topic Watch Topic
  • New Topic

got struck with Long wrapper anonymous object behaviour  RSS feed

 
naveen shrimal
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Long tail=2000L;
Long distance=1000L;
Long story=1000L;
Integer d=900;
System.out.println((story*2)==tail); // here why it prints true is it because the anonymous object is pointing to
same tail object as in case of String immutaable class


Long h=story*2;
System.out.println(h==tail); // it prints false



can some 1 throw light on it
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
naveen shrimal wrote:
System.out.println((story*2)==tail); // here why it prints true is it because the anonymous object is pointing to
same tail object as in case of String immutaable class


I think you're mistaken. First, there's no such thing as an "anonymous object."

As for what's really happening, I believe that Long * int causes Long to get unboxed to long, int promoted to long, those two longs are multiplied to yield a long result, and then tail is unboxed to a long and compared for equality.

Also, unless you tell the JVM otherwise at startup, only -128..127 wrappers are cached.

Long h=story*2;
System.out.println(h==tail); // it prints false


Because here both h and tail are references, so no unboxing needs to occur, and we see that the two references do not point to the same object.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!