• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

got struck with Long wrapper anonymous object behaviour

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic