| Author |
Did not understand the answer(John Meyers's mock exam)
|
swapnil dangore
Ranch Hand
Joined: Jun 05, 2006
Posts: 46
|
|
Hi all , I didn't understand the answer of the following question .Question is from John Meyers's scjp5 mock exam. class test { public static void main(String[] args) {test inst_test = new test(); int i1 = 2000; int i2 = 2000; int i3 = 2; int i4 = 2; Integer Ithree = new Integer(2); // 1 Integer Ifour = new Integer(2); // 2 System.out.println( Ithree == Ifour ); inst_test.method( i3 , i4 ); inst_test.method( i1 , i2 ); } public void method( Integer i , Integer eye ) { System.out.println(i == eye ); } } a. true false true b. false true false c. false false false d. true true false e. Compile error Correct Answer is (b)--- false true false Here is the explanation : lthree and lfour are two seperate objects. if the lines 1 and 2 were lthree = 2 and lfour = 2 the result would have been true. This is when the objects are created in the pool. When the references I and eye in the pool are compared 2==2 results in true and 2000==2000 is false since it exceeds 127. I'm particularly not getting line "since it exceeds 127." Thanks in advance
|
Cheers<br />-------------<br />Swapnil<br /> <br />SCJP5-81%<br /> <br />"Dictionary is the only place where Success come before Work"
|
 |
Sanjeev Singh
Ranch Hand
Joined: Nov 01, 2006
Posts: 381
|
|
All Integer object created (not using new) via valueOf() or simple assignement are designed to be true for == check if their primitive values are same and it ranges from -128 to 127. For example If the value of i1,i2,i3,i4 are not in this range, the equality == check returns false irrespective of their interger value being same.
|
~Sanjeev Singh<br />SCJP 1.5
|
 |
Aniket Patil
Ranch Hand
Joined: May 02, 2006
Posts: 218
|
|
From the JLS:
If the value p being boxed is true, false, a byte, a char in the range \u0000 to \u007f, or an int or short number between -128 and 127, then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.
Remember this will not work if you do a "new", since new will return a different object. [ December 14, 2006: Message edited by: Aniket Patil ]
|
SCJP 5.0 | SCWCD 1.4 <br /> <br />If you don't know where you are going, any road will take you there!
|
 |
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
Guys, What happens when we try to compile it using a java 1.4 compiler. It should print false, true, true...Am I right??I don't have a 1.4 JRE...Can anyone confirm this?
|
SCJP 1.4, SCWCD 1.4 - Hints for you, SCBCD Hints - Demnachst, SCDJWS - Auch Demnachst
Did a rm -R / to find out that I lost my entire Linux installation!
|
 |
swapnil dangore
Ranch Hand
Joined: Jun 05, 2006
Posts: 46
|
|
Thanks all.... But I still have a doubt in my mind......Why range is -128 to 127 for an integer......I can understand it for byte.....
|
 |
Deepak Bala
Bartender
Joined: Feb 24, 2006
Posts: 6588
|
|
Hi swapnil. Thanks for trying the exam. The pool range for Integer, Byte, Short is from -128 to 127. This is just the pool range that we are talking about. If the range had been bigger you wouldnt be able to assign Byte values within that range into the pool. For example if the range were from -1024 to 1023 you cant assign a Byte within this range since the range exceeds the range of a byte. I guess the developers wanted to select a range such that they can also point Integers and Shorts to this common pool. Thats just my hunch anyway but thats just the way it is
|
SCJP 6 articles - SCJP 5/6 mock exams - SCJP Mocks - SCJP 5 Mock exam (Word document ) - SCJP 5 Mock exam in Java.Inquisition format
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
"swapnil," Welcome to JavaRanch! Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name. You can edit your display name here. Thank you for your prompt attention, and enjoy the ranch! -Marc
|
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
|
 |
swapnil dangore
Ranch Hand
Joined: Jun 05, 2006
Posts: 46
|
|
Thanks John....
|
 |
Pratap koritala
Ranch Hand
Joined: Sep 27, 2006
Posts: 251
|
|
not for scjp 1.4 Interger i1=121; is a compiler error in JRE 1.4.X
|
 |
Scott Johnson
Ranch Hand
Joined: Aug 24, 2005
Posts: 518
|
|
not for scjp 1.4
That's correct. Autoboxing is a feature that was added in JSE 5.
|
 |
 |
|
|
subject: Did not understand the answer(John Meyers's mock exam)
|
|
|