Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

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: 23928
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
 
The harder I work, the luckier I get. -Sam Goldwyn So tiny. - this ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic