• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Integer == Integer?

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following question from John Meyers' test,



My answer is false true true. The answer is false true false? Why the last one is false?
 
Sheriff
Posts: 9697
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
Well henry if you create an Integer using autoboxing (i.e. the syntax Integer.valueOf()), then if the value is in the range of -128 to 127, then the Integer object created will be taken from an Integer pool of objects.

So

Integer i1 = 10;
Integer i2 = 10;

will refer to the same object. This is why i1 == i2 will result in true. But if the value is out of this range, then the new objects will be created every time.

So

Integer i1 = 2000;
Integer i2 = 2000;

will refer to different objects. This is why i1 == i2 this time will result in false.

Also remember that using the new Integer() syntax will always result in a new object no matter whatever the value is.

Also as far as I know, this pooling applies to Byte and Short too.
 
Henry Zhi Lin
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your answer, I did not see this on my SCJP books. I will remember it.

Thanks agian.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For reference, see JLS - 5.1.7...

If the value p being boxed is true, false, a byte, a char in the range \u0000 to \u007f, or an int or short number between -128 and 127, then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.


Note that this applies only to autoboxing, so be sure to note what Ankit said above: Whenever you create a new instance using "new," you will get a new object.
 
I am mighty! And this is a mighty small ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic