Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Wrapper Object Equality Test

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm currently studying for the SCJP exam (or OCP JSEP now maybe?) and am having issues with the objective that deals with Wrappers/Boxing. I understand that Java sees Wrapper Objects as two separate objects and also as meaningful equal.

My question is why does it consider the Objects not equal if the value is large, but equal if the value is small? Shouldn't it see both as not equal since they are different objects? See below for the code I used to test (this was done in TextPad).

Integer l4 = 3;
Integer l5 = 3;

Integer i3 = 1000;
Integer i4 = 1000;

void longtest()
{
if(l4 != l5) System.out.println("different objects @ 3"); //no output, makes sense

if(i3 != i4) System.out.println("different objects @ 1000"); //output?!?
}

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

if the value of 1000 is replaced with 127, it doesnt show them as different objects.
but for 128, it still shows that they are different..

it seems to be somehow related to the value of Integer wrapper objects being limited from -128 to 127.
not very sure of the exact answer though.
waiting for some expert's reply.
 
author
Posts: 23956
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

Just search JavaRanch for "integer cache"... this topic comes up a lot.

Henry
 
Rikesh Desai
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Henry!
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is well explained in Kathy and Bert ' s SCJP book at the page 246 as well.

Thanks,
Kushan
 
Jan tenpas
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't see the response on page 246. Can you post the quote that references integer caching on page 246? Maybe I just have an older version or something...

Either way, looks like Henry nailed. Thanks!

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