• 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

Integer comparison

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

Integer i1=127;
Integer i2=127;
System.out.println(i1==i2);



It returns true.

Integer i1=128;
Integer i2=128;
System.out.println(i1==i2);



It returns false.

Why ?
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
upto value 127 compiler will not create any new Integer.
This is the reason for this descripency. If you use new operator for creating Integers then you will get false in both cases.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
to extend what shubhashish said, when a value in the range -128 to 127 is autoboxed to Byte, Short or Integer, then a new object is not created. The object is retrieved from a pool of integer objects...
[ December 09, 2008: Message edited by: Ankit Garg ]
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll give you a couple of hints.

"==" -when applied to objects, as it is here- does not test the equality of the integer values; it test the equality of object references.

So the question becomes "Why is the same object assigned to i1 and i2 in the first case, while different objects are assigned in the second case?"

The key to this is to know that with a statement like "Integer i = 127", the Integer.valueOf(int) method is used to create the Integer object (Note that "127" is not an "Integer" - it is an "int", so it can't be assigned directly to an Integer reference like "i"; the compiler needs to create code that creates an Integer from an int. This is called "autoboxing".)

I'll now let you go read the javadocs of Integer.valueOf; it contains a subtle hint why it sometimes returns the same object for the same int, and sometimes different objects. I'm sure this doesn't answer all your questions, so come back here to ask for clarification.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java doc says if you donot require new Integer object, then you should use this method instead of new Integer(), as it better utilizes time and space.

From the example I think up to value 127 it is not creating any new Integer object, but for value > 127 it is creating new Integer object.

Does JVM maintains some types of pools for the range of -128 to 127 that is the range of a byte, so it does not create any new objects for that range ?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Does JVM maintains some types of pools for the range of -128 to 127 that is the range of a byte, so it does not create any new objects for that range ?


Precisely. If you want to know all the details, you can take a look at the source code of the Integer class. You'll find it in a file called "src.zip", which is somewhere in your JDK installation directory.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I got it.

static final Integer cache[] = new Integer[-(-128) + 127 + 1];

static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Integer(i - 128);
}



From Character class to Long class all are maintaing this cache.
Float and Double are not maintaining any cache.
Thank you very much Ulf.
[ December 09, 2008: Message edited by: Punit Singh ]
 
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ulf Dittmer for such a nice explanation.
 
reply
    Bookmark Topic Watch Topic
  • New Topic