• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Error found in KB's pg236

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
i found a error in katthy's book on page no. 236.
code:
Integer x=10;
Integer y=10;
if(x == y)
System.out.println("Same Object");

this is the code..n the output in the book is "Same Object"..
which is actually wrong because == operator looks that both the reference variables are pointing to the same object or not.
but here both these ref variables are pointing 2 their respective objects.
so this is wrong....
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry but you're wrong !
Remember:
Two instances created without constructor (Integer i1=10) of the following wrappers will always be == when their primitive values are the same:

Boolean
Byte
Short and Integer from -128 to 127
Character from \u0000 to \u007f
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rupali singh:
hi,
i found a error in katthy's book on page no. 236.
code:
Integer x=10;
Integer y=10;
if(x == y)
System.out.println("Same Object");

this is the code..n the output in the book is "Same Object"..
which is actually wrong because == operator looks that both the reference variables are pointing to the same object or not.
but here both these ref variables are pointing 2 their respective objects.
so this is wrong....



Did you try to run it ? The SCJP study guide mentions the reason for the output and the use of pools somewhere in the book. Go through it and you will understand why the result is what it is.

Good luck with your preparation
 
rupali singh
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes i tried it but there was no output.
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
post your code here then.you might have done something else.
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rupali singh:
yes i tried it but there was no output.



I copied the code just as it was and it seemed to produce the desired output. What version of java are you using ?
 
Author
Posts: 587
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Errata points as listed from their errata page (http://www.mhprofessional.com/downloads/products/0072253606/0072253606_errata0504.txt)

236....clarify...Add this sentence: When == is used to compare a primitive to a wrapper object, the wrapper object will be unwrapped to its primitive value, and the comparison will be made between the two primitives' values.

236....clarify...Sentence before bullet points s/b: In order to save memory, two instances of the following wrapper objects, created through autoboxing, will always be equal...
[ June 25, 2008: Message edited by: Robert Liguori ]
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rupali singh:
hi,
i found a error in katthy's book on page no. 236.
code:
Integer x=10;
Integer y=10;
if(x == y)
System.out.println("Same Object");


Nope.It's not wrong.
Output is "Same object";
Try this:
The answer will be false.
Why? If you read on you will find that for the wrappers:
Boolean, Byte, Character, Short and Integer
Two Wrapper objects created through boxing with values >= -128 or <= 127 will refer to the same object to save memory. Check out Java's constant pool.
best regards.
 
rupali singh
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
i m extremly sorry .u guys are correct...
instead of 10 i was taking 1000 which is out of range...i m very sorry

thanks 4 rectifying me
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rupali ,

In order to svae memory , there are some rules about autoboxing .
Fot Byte , Short and Integer , if literal value is in range of byte

Integer a = 100 ;
Integer b = 100 ;

a==b is true because one object of type Integer will be created and both
a and b will refer to this object .

Byte x = -10 ;
Byte y = -10 ;

x==y is true according to above rule .

For more information , Please refer to Java Language Spec 3rd edition .
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic