This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question 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 "Question" Watch "Question" New topic
Author

Question

Vidya Singh
Greenhorn

Joined: Mar 21, 2007
Posts: 28
src:
http://technoheads.blogspot.com/
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

Answer 2:
b: false true false. 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 do not understand--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.
[ May 09, 2008: Message edited by: Jesper Young ]
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Vidya,

This is explained with excellent detail at this link.

Basically, Java 5 introduced wrapper class caching. For int values -128 through 127, inclusive, there is a cache such that an Integer with that int value will come from the cache, so all Integers with a value in that range with the same intValue() will be the same object. That's why the call to method with i3 and i4 returns true for "==" comparison of the Integers created by autoboxing.

Because i1 and i2 are greater than 127, they don't benefit from the cache, so a new object is created for each in the call to method, and the "==" returns false, as they are different objects. If you check i.equals(eye), then both calls to method() will return true.


There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

See our SCJP FAQ: Why do separate autoboxing conversions sometimes return the same reference?


"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
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Originally posted by Vidya Singh:

I do not understand--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 am not sure what your question because you have already explained the answer.
In line 1 and line 2 you are explicitly creating and object using the word "new" ,so there is no possiblity of being referenced to the same object even if they donot exceed 127.
In the second int is being autoboxed to Integer and there fore both point to the same object
and in the third case since it is more then 127 ,they are two different object.
[ May 09, 2008: Message edited by: Ulf Dittmer ]

The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14195
    
  20

Please do not enclose your whole post in [ code ] ... [ /code ] tags.

[ UD: I have removed those tags in order to preserve the layout. Just in case someone is wondering what Jesper is talking about - he's not seeing ghosts :-) ]
[ May 09, 2008: Message edited by: Ulf Dittmer ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
I am not able to get what you guys are telling. Can you elaborate in simple terms.. please....


Thanks<br />Dinesh
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Dinesh Tahiliani:
I am not able to get what you guys are telling. Can you elaborate in simple terms.. please....

Did you read the FAQ entry linked to above?
 
GeeCON Prague 2014
 
subject: Question