• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Boxing - from k&b

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


From K&B, pg: 258:

Using == with wrappers is tricky; wrappers with the same small values (typically lower than 127), will be == , larger values will not be ==.



but, it is not the case

Integer i9 = new Integer(100);
Integer i10 = new Integer(100);
System.out.println("(small values) " + (i9==i10));//false


Integer i11 = new Integer(1000);
Integer i12 = new Integer(1000);
System.out.println("(large values) " + (i11==i10));//false

both return false.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the case when both references refer to the objects created using
autoboxing instead of using new operator.

Integer i1=100; //autoboxed
Integer i2=100; //autoboxed


(i1==i2)//true


That is play of words as you refer to the lines of K&B.

Using new operator to create object means a new object will be created
on the heap and its reference is assigned to the ref variable. Each time
new is used, new object is created and consequently new reference is
returned.




Thanks,
[ May 24, 2007: Message edited by: Chandra Bhatt ]
 
author
Posts: 23919
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
From your subject header, you imply that K&B is discussing autoboxing. But in your example, which is intended to prove K&B wrong, you don't use anying autoboxing.

Basically, those statements in K&B is true if all the objects were achieved by autoboxing. If you create two different objects explicitedly, those two different objects will be... well... two different objects.

Henry
 
If you two don't stop this rough-housing somebody is going to end up crying. Sit down and read this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic