Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Objects passed by reference

 
Ranch Hand
Posts: 56
Tomcat Server Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In reference to the question and answer below, got asked this question in the rules roundup. I don't quite agree though.

Why would java pass a copy of the reference to the original object? It doesn't makes sense, I thought that a copy of the reference gets passed to the method. Or am I not understanding the question?

QUESTION:
=========

(#8) When you pass a reference to an array as an
argument to a method call, you pass a new reference
to the original array.

Answer: true
An array is an object, and Java always passes a copy of
the reference to the original object. Both references now
point to the same array object.
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer has the potential to be misunderstood, due to its phrasing, and I think you have done so.

Using square brackets to split up the clauses (if that's the right term), I think the writer meant: -

"An array is an object, and Java always passes a copy of
[the reference to the original object]."

... whereas it seems you think they meant: -

"An array is an object, and Java always passes a copy of
[the reference] to the original object."
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Peter Chase:
..."An array is an object, and Java always passes a copy of
[the reference to the original object]."...


Maybe it should be rephrased to something like: "A reference to the original object is passed to the method."
 
Michael Labuschagne
Ranch Hand
Posts: 56
Tomcat Server Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peter, that does make a lot more sense. But now, back to question. If you read the part I've bracketed, below, it seems to imply that the reference itself is changing. Why "new reference", as if a new reference is being created as a result of passing the object to the method?

Thanks you're a great help.

QUESTION:
=========

(#8) When you pass a reference to an array as an
argument to a method call, (you pass a new reference
to the original array).
 
Marshal
Posts: 25669
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Michael Labuschagne:
Why "new reference", as if a new reference is being created as a result of passing the object to the method?

Because a new reference IS being created. That new reference is assigned to the parameter.
 
Don't count your weasels before they've popped. And now for a mulberry bush related tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic