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?
Joined: Sep 19, 2004
Nothing left out; too much information. Actual answer:
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.
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.