• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Passing by value vs. reference

 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just perusing this thread again....

Originally posted by Rick Portugal:
Ideally Java would always pass parameters the same way.


It does! How cool is that?
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Steve Morrow:
Just perusing this thread again....

It does! How cool is that?



Wow! Java is IDEAL!
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow. I go away for a weekend and what should be a simple answer blows up into a multipage thread.

1) Java always passes by value.

2) A variable of any non-primitive type is a reference to an object. You never actually hold an object.

3) When you pass anything into a method a copy is made that is local to that method. In the case of primitives a copy of the primitive is made, in the case of references a copy of the reference is made. In the act of being copied they are being passed by value (just to drive home point 1).

4) There are several classes that are immutable. They are pretty easy to spot. The method signature will give them away when they return the same type as themselves. If all else fails look at the javadoc, it's there for a reason.

Did I leave anything out?
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nothing left out; too much information. Actual answer:

Java is pass by value, period.
 
bronco
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow...what a thread.

I may have scanned it too quickly, but has anyone pointed out that the discussion of String being immutable is totally irrelevant to the original question?

There's no difference between these two in terms of how the parameters work:

void foo(String s) { s = "abd"; }
void foo(SomeNonImmutableClass x} { x = new SomeNonImmutableClass(); }

[ as an aside, changing the values of input parameters is generally bad style, IMHO! (leads to the type of confusion exhibited in this thread) ]

-Dave
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dave Wood:

I may have scanned it too quickly, but has anyone pointed out that the discussion of String being immutable is totally irrelevant to the original question?



I mentioned that in my first post, but it's worth mentioning again.

One person asserted that Strings immutability was the reason for the confusion in the original post. That's how it came up.
 
Steve Morrow
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dave Wood:
Wow...what a thread.



That's nothing...

http://forum.java.sun.com/thread.jspa?threadID=575095
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java passes all types (reference and primitive) by value.
The authoritative source says so
http://java.sun.com/docs/books/vmspec/2nd-edition/html/Concepts.doc.html#26454

Further explanation
http://qa.jtiger.org/GetQAndA.action?qids=37&showAnswers=true
 
Steve Morrow
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We gots links...

JVM �2.10 Methods
A method declares executable code that can be invoked, passing a fixed number of values as arguments.

JVM �2.10.1 Formal Parameters
The formal parameters of a method, if any, are specified by a list of comma-separated parameter specifiers. Each parameter specifier consists of a type and an identifier that specifies the name of the parameter. When the method is invoked, the values of the actual argument expressions initialize newly created parameter variables (�2.5), each of the declared type, before execution of the body of the method.

JVM �2.5 Variables
A variable is a storage location. It has an associated type, sometimes called its compile-time type, that is either a primitive type (�2.4.1) or a reference type (�2.4.6). A variable always contains a value that is assignment compatible (�2.6.7) with its type. A variable of a primitive type always holds a value of that exact primitive type. A variable of reference type can hold either a null reference or a reference to any object whose class is assignment compatible (�2.6.7) with the type of the variable.

JLS �4.1 The Kinds of Types and Values
There are two kinds of types in the Java programming language: primitive types (�4.2) and reference types (�4.3). There are, correspondingly, two kinds of data values that can be stored in variables, passed as arguments, returned by methods, and operated on: primitive values (�4.2) and reference values (�4.3).

JLS �4.3.1 Objects
The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object.

JLS �4.5.3 Kinds of Variables
Method parameters (�8.4.1) name argument values passed to a method. For every parameter declared in a method declaration, a new parameter variable is created each time that method is invoked (�15.12). The new variable is initialized with the corresponding argument value from the method invocation.

JLS �8.4 Method Declarations
A method declares executable code that can be invoked, passing a fixed number of values as arguments.

JLS �8.4.1 Formal Parameters
When the method or constructor is invoked (�15.12), the values of the actual argument expressions initialize newly created parameter variables, each of the declared Type, before execution of the body of the method or constructor.

The Java� Tutorial
In Java methods, arguments are passed by value. When invoked, the method receives the value of the variable passed in. When the argument is of primitive type, pass-by-value means that the method cannot change its value. When the argument is of reference type, pass-by-value means that the method cannot change the object reference, but can invoke the object's methods and modify the accessible variables within the object.

http://javadude.com/articles/passbyvalue.htm
http://java.sun.com/developer/JDCTechTips/2001/tt1009.html#tip1
http://www.javaranch.com/campfire/StoryPassBy.jsp
http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html
http://www-106.ibm.com/developerworks/library/j-praxis/pr1.html
http://www.cs.toronto.edu/~dianeh/tutorials/params/
http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#38698
http://radio.javaranch.com/channel/val/2004/05/21/1085125887000.html

From "the man himself"...

All parameters to methods are passed "by value." In other words, values of parameter variables in a method are copies of the values the invoker specified as arguments. If you pass a double to a method, its parameter is a copy of whatever value was being passed as an argument, and the method can change its parameter's value without affecting values in the code that invoked the method. For example:

The following output illustrates that the value of arg inside halveIt is divided by two without affecting the value of the variable one in main:You should note that when the parameter is an object reference, the object reference -- not the object itself -- is what is passed "by value." Thus, you can change which object a parameter refers to inside the method without affecting the reference that was passed. But if you change any fields of the object or invoke methods that change the object's state, the object is changed for every part of the program that holds a reference to it. Here is an example to show the distinction:

This program produces the following output: Notice that the contents of the object have been modified with a name change, while the variable sirius still refers to the Body object even though the method commonName changed the value of its bodyRef parameter variable to null. This requires some explanation.

The following diagram shows the state of the variables just after main invokes commonName:

At this point, the two variables sirius (in main) and bodyRef (in commonName) both refer to the same underlying object. When commonName changes the field bodyRef.name, the name is changed in the underlying object that the two variables share. When commonName changes the value of bodyRef to null, only the value of the bodyRef variable is changed; the value of sirius remains unchanged because the parameter bodyRef is a pass-by-value copy of sirius. Inside the method commonName, all you are changing is the value in the parameter variable bodyRef, just as all you changed in halveIt was the value in the parameter variable arg. If changing bodyRef affected the value of sirius in main, the "after" line would say "null". However, the variable bodyRef in commonName and the variable sirius in main both refer to the same underlying object, so the change made inside commonName is visible through the reference sirius.

Some people will say incorrectly that objects are passed "by reference." In programming language design, the term pass by reference properly means that when an argument is passed to a function, the invoked function gets a reference to the original value, not a copy of its value. If the function modifies its parameter, the value in the calling code will be changed because the argument and parameter use the same slot in memory. If the Java programming language actually had pass-by-reference parameters, there would be a way to declare halveIt so that the preceding code would modify the value of one, or so that commonName could change the variable sirius to null. This is not possible. The Java programming language does not pass objects by reference; it passes object references by value. Because two copies of the same reference refer to the same actual object, changes made through one reference variable are visible through the other. There is exactly one parameter passing mode -- pass by value -- and that helps keep things simple.

-- Arnold, K., Gosling J., Holmes D. (2000). The Java� Programming Language Third Edition. Boston: Addison-Wesley.
 
Steve Morrow
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I love the smell of wasting bandwidth in the morning...
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Out of curiosity, is there anyone still reading this thread who thinks that java doesn't pass by value? It seems we're just getting a lot of repetition of the same basic points here.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic