• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Stupid question: How to pass by value?

 
Justin van Wilgen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know in java things are normally passed by reference, but what would be the most efficient way to pass by value? Assuming the object class has a copy method would that do the trick? =/

Thanks
 
Paul Clapham
Sheriff
Pie
Posts: 20719
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, let's not start from a false premise. Parameters are always passed by value in Java; read http://www.coderanch.com/how-to/java/CallByReferenceVsCallByValue to find out more about that.

And when you have clarified your understanding about that topic, I would recommend you recast your question into a question about what you wanted to do, rather than a question about something you already chose which didn't do that anyway.

And welcome to the Ranch!
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're probably thinking of defensive copying. You can't reference objects directly in Java: you have to use a reference variable, which is just a memory address that sits on the stack. It gets passed by value when you use it as the argument in a method; however this value is still the address of the object, so if you do anything with the new reference variable you'll still be pointing at the same object, giving the appearance of having passed the object itself by reference.

If your method involves altering the input object to produce a result (sorting a list, for example), but you don't want to change the original, you'd make a copy within the method. If your class supports it (most in the standard API do) you can call the clone() method - although cloning should generally be avoided. Some objects (collections especially) have constructors that can take another instance and make a copy. The way you make the copy in general depends on the object's concrete type.

E.g. (ignore the generics if you want - it just means we can use the method on any list that we can sort)
will give the result

4
emu
[10, 12, 3, 4, 5]
[cod, mouse, zebra, emu]

But if you don't make the copy and instead run the sort on the reference that was passed, i.e.you'll find we've inadvertently changed the argument we passed in get the result

4
emu
[3, 4, 5, 10, 12]
[cod, emu, mouse, zebra]

because the reference "a" still points to the original object.

This problem can be overcome by using immutable objects, which is the default for functional languages. However, Java doesn't really do immutable.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin van Wilgen wrote:I know in java things are normally passed by reference, ...

That's wrong - in Java, method arguments are always passed by value, and never passed by reference.

Many people are confused by this, because variables of non-primitive types are references in Java. When you pass such a variable to a method, it is still passed by value - but you are passing a reference by value; in other words, a copy of the reference is made. But passing a reference by value is not the same as passing by reference!
 
Justin van Wilgen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys, My ignorance has been cured! Much appreciated
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic