File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Autoboxing with equal method

 
Andry Dub
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! Please anybody explain me why 1st code produces "equal", but 2nd - "not equal". Where Autoboxing and Autounboxing take place?




 
ahmed yehia
Ranch Hand
Posts: 424
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
equals() compare values, "==" check refrences.

Auto boxing took place just here: Integer i = 5; int 5 is autoboxed to Integer.
 
Deepak Jain
Ranch Hand
Posts: 637
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Try this code and the output will be
equal
equal

The reason is when Integer i = value;
and if the value is between -127 and 128 the value is cached and re-used and hence the result is equal in second case.
However if new Integer(5) is done then JVM will always create a new object no matter the value in the constructor is within the save memory range [-127 to 128]
Thanks
Deepak
 
Fu Dong Jia
Ranch Hand
Posts: 131
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi!
go here please explain,may got you clear.
 
Andry Dub
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everybody!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic