wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Error found in KB's pg236 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Error found in KB Watch "Error found in KB New topic
Author

Error found in KB's pg236

rupali singh
Greenhorn

Joined: Jun 20, 2008
Posts: 9
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....
Antonio Tercero
Ranch Hand

Joined: Jun 05, 2008
Posts: 110
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


SCJP 5, SCWCD 5
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

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


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
rupali singh
Greenhorn

Joined: Jun 20, 2008
Posts: 9
yes i tried it but there was no output.
sachin verma
Ranch Hand

Joined: Mar 25, 2008
Posts: 177
post your code here then.you might have done something else.


SCJP || SCWCD

Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

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 ?
Robert James Liguori
Author
Ranch Hand

Joined: Nov 27, 2007
Posts: 531
    
    5

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 ]

My Blog (I frequently post on Java, JSF and NetBeans)
Keith Nagle
Ranch Hand

Joined: May 06, 2008
Posts: 65
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.


SCJP 5.0
rupali singh
Greenhorn

Joined: Jun 20, 2008
Posts: 9
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
Madhukar Ojha
Ranch Hand

Joined: Mar 21, 2007
Posts: 71
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 .


SCJP 5 ๑۩۞۩๑♥~~ My Life is My Creation ~~♥๑۩۞۩๑
 
GeeCON Prague 2014
 
subject: Error found in KB's pg236