• Post Reply Bookmark Topic Watch Topic
  • New Topic

Integer  RSS feed

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
author
Sheriff
Posts: 23555
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Sushant Kaushik
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all. My doubt got clarified
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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?
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Maybe he went home and went to bed. And took this tiny ad with him:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!