• 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

Autoboxing/unboxing

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read that certain primitives are always to be boxed into the same immutable wrapper objects and these objects are then cached and reused, with the expecation that these are commonly used objects. These special values are :
* boolean values true and false
* byte values
* short and int values between -128 and 127
* char values in range '\u0000' to '\u007f'

So should I assume that for boxing/unboxing, values of short/int/byte range between -128 to 127?

If thats the case, then why is the following TRUE when both are out of range?

Integer k=128;
int kk = 128;
System.out.println(k==kk);

Thanks in advance.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The value until 127 is using comparing two wrappers.

Integer i1 = 127;
Integer i2 = 127;

System.out.println(i1 == i2) // true

but

Integer i1 = 128;
Integer i2 = 128;

System.out.println(i1 == i2) // false, don't have the same reference


You can try with Short, Long, but what I saw even Float and Double has the number until 127, they will be false.
 
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
From 5.1.7 of the Java Language Specification...

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... Ideally, boxing a given primitive value p, would always yield an identical reference. In practice, this may not be feasible using existing implementation techniques. The rules [linked to] above are a pragmatic compromise...


In other words, two autoboxed instances of Integer, for example, will have equal references (==) if their wrapped values are equal and within byte range, -128 to 127. Outside of this range, there is apparently no guarantee.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To take this a little further...

I read 15.21.1 from the spec, which uses 5.1.8 and 5.6.2 in support, to mean that when you use == to compare a primitive to a wrapper, the wrapper will be unboxed, then the two primitives will be compared.

It looks like we need to add a sentence to our book...dang!

Good catch Krishna!
 
Krishna Mir
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everybody. I got the point.

Krishna
 
Catch Ernie! Catch the egg! And catch this tiny ad too:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic