Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question

 
Vidya Singh
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See our SCJP FAQ: Why do separate autoboxing conversions sometimes return the same reference?
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not able to get what you guys are telling. Can you elaborate in simple terms.. please....
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic