• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Object References Help Please

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All:
I am confused by two questions that have surfaced in my mock exams. Read on for your entertainment pleasure and please assist this greenhorn

With this question I am taught that when an Object reference is passed to a method, the copy of the object reference is received as the method argument. The copy also refers to the original object itself. Since arrays are objects, the change made within the g()method to the int[]c argument (LINE 2) affects the original array object (int[]c on LINE 1), so the value of the first element of the original version of the array changes to 1.

Fine. now....

Here I am told that on LINE 2, the method f() makes the local variable s refer to null(LINE 1), but that this does not affect the original variable in the main function. 'Scuse me?? How can when something happens in a method to an array, which is an Object, also affect the original version of that array, but yet when a String, which also is an object, gets manipulated by a method, the change is not reflected back on the orginal version?
Can someone please clarify?
Thanks to all for your time and response
g

[ May 16, 2007: Message edited by: G Marshall ]

[ May 16, 2007: Message edited by: G Marshall ]


EDIT by mw: Added Code Tags.

[ May 16, 2007: Message edited by: marc weber ]

(quietened down shout in topic title - barry)
[ May 17, 2007: Message edited by: Barry Gaunt ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When a reference copy is passed to a method, it's assigned to a local variable. As you saw in your first code example, the local variable references the same object as the original variable, and you can manipulate the object's state (assuming the object is mutable) using either variable.

But the second example is a bit different. When the reference copy is first passed into the method, the variable "s" in f and the variable "s" in main (which are different) both point to the same object (a String array). At that point, the array could be manipulated using either variable, just as in the first example. But instead, the variable "s" in f is reassigned to a null reference. This reassignment has no effect on the variable in main, which is still pointing to the String array.
 
Gary Marshall
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
marc:
Thank you for taking the time.

So, if I am understanding you correctly, it looks like I need to understand whats happening to the reference variables and what is happenning to the objects.

The code in the method f()is just changing the "s" variable to point to something else, in this case null, from what it was pointing to, which was the string[] that was passed in, and therefore, since its messing with the variable and not the object (the string []), the String[] remains untouched. And since the "s" variable in method f() is a copy of the reference variable "s" in main, then f() is just changing its copy of the variable that was sent in.

Am I on the right track here?
thanks again for your assistance
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by G Marshall:
...Am I on the right track here? ...


That's exactly right.

In the first example, the variable's value (a reference) is just being used to "get to" the object. But in the second example, the variable's value is actually being changed (reassigned to null).
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you already see this, but it's worth pointing out...

In the first example, the variable 'c' references an int array. But c[x] is an element in that array -- in this case, an int value. So assigning a new value to c[x] (for example, c[0]=1) is just changing one of the array's elements. It does not change which object c itself is referencing.
[ May 16, 2007: Message edited by: marc weber ]
 
Gary Marshall
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marc:
I'm back after having slept on this one and thought about it for a day.

You would think this concept would be naturally understood, but...

So, going back to my second coding example, if the f() method were to, instead of setting the "s" variable to null, rather it did something like.... "s[1] = "element2"; ", then this would affect the "s" variable (the String[]) back at the main, just like what happens in my first coding example?

Thank you again for your time.
 
We don't have time to be charming! Quick, read this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic