• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

behavior of Integer object when passed as parameter to method

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I can't wrap my head around the idea that myObject is not changed to 5 and still remains with the value of 2?  In other words, I think that this should output "5 5", but instead it outputs "2 5".

The way that I understand this, myObject is a reference variable.  It points to an object on the heap that has an instance variable with the value of 2 at the beginning of the program.  What really happens in the MyHeadHurts method when myPrimitive is assigned to myObject?  It changes the value in the object's instance variable from 2 to 5.  Why is this not reflected in the value of myObject's instance variable in the main method?

In other words, I realize that in the MyHeadHurts method I have a copy of the address to the object, but isn't it still pointing at the same instance variable on the heap?  I hope I have explained this clearly.  
 
Saloon Keeper
Posts: 9734
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
  • myObject is a LOCAL reference only known within MyHeadHurts().
  • It is a COPY of the reference passed in from main() using (coincidentally) main()'s variable named myObject.
  • When first entering the MyHeadHurts() method, both the method's myObject and main()'s myObject refer to the same Integer instance at this point.
  • The assignment operator creates a new Integer object via implicit "auto boxing" and assigns its reference to the LOCAL variable myObject.
  • Now the method's myObject and main()'s myObject DO NOT refer to the same Integer instance.
  •  
    Robin Z. Clark
    Greenhorn
    Posts: 29
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Perfect explanation!  My confusion was that I did not understand the assignment operator creates a new Integer object via implicit autoboxing.  

    I am going to play around with some different scenarios that I think will help solidify my understanding.
     
    Carey Brown
    Saloon Keeper
    Posts: 9734
    80
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The Integer class is immutable. You cannot modify the internal state of an instance given a reference to it. The state can only be set during construction. Therefore
    "Some expression" must always result in a Integer reference which will replace the reference currently held in 'v' by the assignment operator. The fact that your expression was a primitive is what caused the auto-boxing, and auto-boxing causes a new object to be constructed using the primitive as a parameter..
     
    Sheriff
    Posts: 7123
    184
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Just a side note: new Integer(2) is now deprecated.  Use Integer.valueOf(2) instead.
     
    He does not suffer fools gladly. But this tiny ad does:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic