• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Stupid question: How to pass by value?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, let's not start from a false premise. Parameters are always passed by value in Java; read https://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!
 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys, My ignorance has been cured! Much appreciated
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic