• 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

Doubt in Wrapper

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having a doubt in this section.Please help me

The code goes here

Integer i1=10;
Integer i2=10;
if(i1 == i2)
System.out.println(" i1 and i2 are equal");

Output is:
i1 and i2 are equal

Integer i1 = 1000;
Integer i2 = 1000;
if(i1 != i2) System.out.println("Different objects");
Output is:
Different Objects

How can this be possible



 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Please refer to these posts for your answer

https://coderanch.com/t/427943/Programmer-Certification-SCJP/certification/Query-with-Boolean#1899129

https://coderanch.com/t/269686/Programmer-Certification-SCJP/certification/Equality#1252595

Thanks
Abhay
 
Ranch Hand
Posts: 282
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java automatically caches integral wrapper objects that are within the byte range [-128,127]. This works for Byte, Short, Character, Integer, and Long, as well as Boolean.

It also does something similar with Strings:
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Java automatically caches integral wrapper objects that are within the byte range [-128,127]. This works for Byte, Short, Character, Integer, and Long, as well as Boolean.



As far as the specification, the JVM is not required to cache Long objects. The specification states that a particular range for particular wrappers must be cached. It doesn't state what should happen to values outside the range, or for other objects... So the JVM is free to cache, or not-cache the values. This is the case with Long objects. The Sun JVM caches them, even though it is not required to.

Henry
 
Michael Angstadt
Ranch Hand
Posts: 282
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:As far as the specification, the JVM is not required to cache Long objects...The Sun JVM caches them, even though it is not required to.


Sorry for my mistake! Thanks for correcting me.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic