• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Same object and same value  RSS feed

 
Ranch Hand
Posts: 37
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How come this makes x and y not only the same value but the same object too? Does x = y automatically convert x into the same object as y? I thought this was only used to assign values not merge objects with one another.
 
Master Rancher
Posts: 4042
47
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'x' and 'y' are references to the Integer objects, not the objects themselves.
When you do x = y you are changing x's reference to be the same as y's.
So they are both pointing at the same object after that line.

It's not changing any values on the objects themselves.
It's changing what the variables are pointing at.
 
He was giving me directions and I was powerless to resist. I cannot resist this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!