• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Did not understand the answer(John Meyers's mock exam)

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
swapnil dangore
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.....
 
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
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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
 
swapnil dangore
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks John....
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
not for scjp 1.4

Interger i1=121;
is a compiler error in JRE 1.4.X
 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

not for scjp 1.4



That's correct. Autoboxing is a feature that was added in JSE 5.
 
Skool. Stay in. Smartness. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic