File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Stupid question: How to pass by value? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Stupid question: How to pass by value?" Watch "Stupid question: How to pass by value?" New topic
Author

Stupid question: How to pass by value?

Justin van Wilgen
Greenhorn

Joined: Jul 06, 2011
Posts: 3
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Jan 06, 2011
Posts: 441

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

Joined: Aug 16, 2005
Posts: 14114
    
  16

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!


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Justin van Wilgen
Greenhorn

Joined: Jul 06, 2011
Posts: 3
Thanks guys, My ignorance has been cured! Much appreciated
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stupid question: How to pass by value?