Java passes everything by value, and not by reference i.e. objects, arrays (which are objects in Java), primitive types (like ints and floats), etc. – these are all passed by value. When passing arguments to a method, Java will create a copy or copies of the values inside the original variable(s) and pass that to the method as arguments. The key with pass by value is that the method will not receive the actual variable that is being passed – but just a copy of the value being stored inside the variable.
Objects are also passed by reference – meaning that a reference/memory address is passed when an object is assigned to another but that reference is actually passed by value. The reference is passed by value because a copy of the reference value is created and passed into the other object. So objects are still passed by value in Java, just like everything else.
One other very important thing to know is that a variable of a class type stores an address of the object, and not the object itself. The object itself is stored at the address which the variable points to. Let’s suppose we have a class called PersonClass and that we instantiate it with a variable of the PersonClass type i.e.
PersonClass someVar;
someVar will just contain a reference – which is basically a memory address that points to the real object and all the class variables for that object actually lives. Suppose we have a class called PersonClass, which has a method called set() that just sets the name and age for a given Person object. Now, let’s say we have the following code:
// Create an object by passing in a name and age: PersonClass variable1 = new PersonClass("Mary", 32); PersonClass variable2; // Both variable2 and variable1 now both name the same object variable2 = variable1; // this also changes variable1, since variable 2 and variable1 name the same exact object. variable2.set("Jack", 22); System.out.println(variable1); Output Jack 22
Both variable1 and variable2 hold a reference to the same object in memory – and this happened when we ran this line of code “variable2 = variable1″. When we use the assignment operator (the “=”) with variables of a class type, then we are copying that reference value – in other words variable2 is given the memory address (or reference) that is being stored in variable1. This assignment of references is best displayed by actual drawings, which we show below:
Example #1
class Operation{ int data=50; void changeVar(int data){ // Changes will be in the local variable only data=data+100; } void changeObj(int data){ //Changes will be in the instance variable this.data=this.data+data; } public static void main(String args[]){ Operation op=new Operation(); op.changeVar(500); System.out.println("after change "+op.data); op.changeObj(500); } }
Example #2
public static void main( String[] args ) { Dog aDog = new Dog("Max"); // we pass the object reference by value to fooValue fooValue(aDog); // aDog variable is still pointing to the "Max" dog when fooValue(...) returns aDog.getName().equals("Max"); // true, java passes by value aDog.getName().equals("Fifi"); // false fooReference(aDog); // when fooReference(...) returns, the name of the dog has been changed to "Fifi" aDog.getName().equals("Fifi"); // true } public static void fooValue(Dog d) { d.getName().equals("Max"); // true // change d inside of foo() to point to a new Dog instance "Fifi" d = new Dog("Fifi"); d.getName().equals("Fifi"); // true } public static void fooReference(Dog d) { d.getName().equals("Max"); // true // this changes the name of d to be "Fifi" d.setName("Fifi"); }