• Post Reply Bookmark Topic Watch Topic
  • New Topic

Surprising behaviour of Integer  RSS feed

 
marwen Bakkar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


outputs :

555
556

This was news to me. a and b are references to the same object so why does this happen? Is there a way to to use Integer like AtomicInteger's set() method without writing a wrapper?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer is immutable. So when you auto-increment b (which is allowed through auto-boxing), a new object needs to be created and you have two distinct objects.
 
marwen Bakkar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Integer is immutable.


Right right I totally forgot about this. Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!