• 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

Is there something wrong ..

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Integer i = 256;
Integer j = 256;

i==j // false

Integer i = 100;
Integer j = 100;

i==j // true

how it can be possible ...

thanks .
 
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
Hmmm... Interesting.

As separate Integer instances, I would expect these to return false under the == comparison. But they seem to return true when the values are within the range of a byte (-128 to 127) and autoboxing is used. If the values are outside the range of a byte or "new Integer(int)" is used, they return false.

Note: I see the same pattern autoboxing Long instances with long literals (for example, Long x = 127L). The unexpected results are still within the byte range.


[ February 05, 2005: Message edited by: marc weber ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This behavior is mandated by JLS3 5.1.7. (Which is admittedly a beta and still subject to change - but assuming it doesn't...) Actually no guarantees are made for ints and shorts outside the range [-128, 127]; that's technically unspecified. But within that range, any two boxing conversions (in the same JVM at the same time) must result in the same wrapper instance. The implementation of this is visible in the source to Integer.valueOf() in JDK 1.5 - though this is not actually guaranteed in the API. Nothing says that boxing must use Integer.valueOf(), to my knowledge - but it looks like that is indeed the code that is used.
[ February 05, 2005: Message edited by: Jim Yingst ]
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Long i = 256;
Long j = 256;

i==j // ???

Long i = 100;
Long j = 100;

i==j // ???

thanks .
 
marc weber
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

Originally posted by Jim Yingst:
This behavior is mandated by JLS3 5.1.7...


Interesting. Thanks for the link!

Based on what I had read previously, I expected autoboxing to provide wrapper instances just as if "new" were used, but I see it's more involved than that.
 
marc weber
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

Originally posted by rathi ji:
...Long i = 100;
Long j = 100;
i==j // ??? ...


I think Jim's post answers this question in general.
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please put the answer ...

thanks a lot .
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not run the code in your JDK to find out? You will need to fix a couple of compile errors of course, but that's good practice, right?

There doesn't seem to be any guarantee in JLS3 5.1.7 of the behavior in the case of long. However the current JDK seems to behave the same way for long that it does for int. Since this isn't guaranteed, I recommend not relying on this behavior. And it's not going to be on the test. (In contrast to the behavior for int within the range [-128, 127] which is guaranteed and therefore may be on the test.)
[ February 07, 2005: Message edited by: Jim Yingst ]
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim , I don't have JDK 1.5 on my machine .
 
marc weber
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

Originally posted by rathi ji:
Jim, I don't have JDK 1.5 on my machine.


Are you not able to download and install 1.5? (Are you using a Mac?)

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic