Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Integer

 
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
MyEclipse IDE Firefox Browser Linux
  • 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
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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.
 
Note to self: don't get into a fist fight with a cactus. Command this tiny ad to do it:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic