File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Integer 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 "Integer" Watch "Integer" New topic
Author

Integer

Sushant Kaushik
Greenhorn

Joined: Dec 12, 2008
Posts: 25
This is test Program -


Output is - false

If I comment Line 1 then output is - true.

Can someone please explain why output is false when Line1 is not commented :roll:
What is the type of j after Line 1? is it still Integer or int? If its Integer then I can understand that output will be false. If its int then Output should be true. correct??
Rafael Angarita
Ranch Hand

Joined: Jan 09, 2009
Posts: 67
The output is false because j and i are not references to the same object.

If you comment Line 1, and check if j == i, the result if true because you are comparing references variables and the refer to the same object.

After Line 1 i still a reference to the Integer object and j just has the an int literal.


Rafael Angarita.
SCJP 6.
Rafael Angarita
Ranch Hand

Joined: Jan 09, 2009
Posts: 67
Actually, I think that in line Line 1, Autoboxing enters in action and creates a new Integer object. Therefore, i and j refer to different Integer objects.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Quite a tricky question. I doubt if it would make it on the real SCJP exam. The exam does expect you to know the difference between == and .equals() but it does not attempt to trick you into giving the wrong answer.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19067
    
  40

Rafael Angarita wrote:Actually, I think that in line Line 1, Autoboxing enters in action and creates a new Integer object. Therefore, i and j refer to different Integer objects.


At line 8 (line 1 in comment), autoboxing actually doesn't create a new Integer object -- it gets the object from the Integer cache. It is the code (at line 6) that creates a new object, instead of getting it from the integer cache. If line 6 also used autoboxing (as below), then it would have compared, as they would have been the same reference from the cache.


Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sushant Kaushik
Greenhorn

Joined: Dec 12, 2008
Posts: 25
Thanks all. My doubt got clarified
Mukherji Sandeep
Greenhorn

Joined: Dec 09, 2008
Posts: 6


Please check it. i and j are not same in this case. But i and j both are still Integer types. so then where is the difference?


Mukherji Sandeep<br />Trying Hard to learn java
Garg Amit
Greenhorn

Joined: Jan 13, 2009
Posts: 21
public static void main(String[] args){

Integer i = new Integer(10);
Integer j =i;
j = j.intValue(); //Line 1
System.out.print("j is instance of an Integer: ");
System.out.println(j instanceof Integer);
System.out.print("i is instance of an Integer: ");
System.out.println(i instanceof Integer);
boolean a = j == i;
if(a) System.out.println("j == i"); else System.out.println("j != i");

if(j.equals(i)){
System.out.println("j == i");
}
}



When you use == on two objects then it compare the bit pattern stored in the object. That's why when you comment the line, the bit pattern stored in both integer object is same. try equals method which compare two object if they are meaningfully equal. Integer class override the equals method so it would work.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Integer