Question

Is java pass by value or pass by reference. My question induced me to write this class so that I may confidently answer. As I was wondering I noticed there might be issue with immutable and mutable object. what I am asking is what is the right answer after looking at the output of this simple class.

class

package notsure.tests;

public class PassingValues {

static Object[] passingValueMethod(int intValue, StringBuilder strValue){
    int recievedIntValue = intValue;
    StringBuilder recievedStrValue = strValue;

    System.out.println("------Let's see mutable objects------");
    System.out.println("----In the called method-------");
    System.out.println("-----New References Without Modification-----");
    //No modification
    System.out.println("Recieved integer: "+recievedIntValue);
    System.out.println("Received StringBuilder: "+ recievedStrValue);
    System.out.println();

    System.out.println("---- New refernces With Modification-----");
    //Modification
    recievedStrValue.append(", I am modified in a method() through a reference ");
    System.out.println("Recieved StringBuilder: "+ recievedStrValue);
    recievedIntValue++;
    System.out.println("Recieved integer: "+recievedIntValue);
    System.out.println();
    //Evaluate the parameter values
    System.out.println("----Received parameter variables current values-----");
    System.out.println("StringBuilder: "+strValue+" \nInteger: "+intValue);
    return new Object[]{recievedIntValue, recievedStrValue};

}
static String passingImmutable(String str){
    String recievedStr = str;
    System.out.println("-----In passpassingImmutable() ------");
    System.out.println("---------without modification------");
    System.out.println("Recieved string with local ref: "+recievedStr);
    System.out.println();
    System.out.println("------With modification-------");
    recievedStr = str+" I am modified";
    System.out.println("Recieved string with local ref: "+recievedStr);
    System.out.println();
    System.out.println("----Let's see the parameter value content---");
    System.out.println("Recieved string with param ref: "+str);
    return recievedStr;
}
public static void main(String[] args) {
    Object[] object = new Object[2];
    int integer = 10;
    StringBuilder stringBuilder=new StringBuilder("Stringbuilder");
    object = passingValueMethod(integer,stringBuilder);
    System.out.println();

    System.out.println("---------Back in Main-------- ");
    System.out.println("----Values returned----");
    for(Object obj:object){
        System.out.println(obj);
    }
    System.out.println();
    System.out.println("----Variables in Main-----");
    System.out.println(integer);
    System.out.println(stringBuilder);
    System.out.println("NOTE: even local Object(except primitive) reference reflect changes");
    System.out.println();
    System.out.println("-----Let's use immutable objects-----");
    String str = "I am a string";
    System.out.println("Value in main before method call: "+str);
    System.out.println();
    passingImmutable(str);
    System.out.println();
    System.out.println("--------------Back in main----------");
    System.out.println("String Value retuned: "+str);
    System.out.println();
    System.out.println("String passed(main reference) value: "+str);

}

}

output

------Let's see mutable objects------
----In the called method-------
-----New References Without Modification-----
Recieved integer: 10
Received StringBuilder: Stringbuilder

---- New refernces With Modification-----
Recieved StringBuilder: Stringbuilder, I am modified in a method() through a reference 
Recieved integer: 11

----Received parameter variables current values-----
StringBuilder: Stringbuilder, I am modified in a method() through a reference  
Integer: 10

---------Back in Main-------- 
----Values returned----
11
Stringbuilder, I am modified in a method() through a reference 

----Variables in Main-----
10
Stringbuilder, I am modified in a method() through a reference 
NOTE: even local Object(except primitive) reference reflect changes

-----Let's use immutable objects-----
Value in main before method call: I am a string

-----In passpassingImmutable() ------
---------without modification------
Recieved string with local ref: I am a string

------With modification-------
Recieved string with local ref: I am a string I am modified

----Let's see the parameter value content---
Recieved string with param ref: I am a string

--------------Back in main----------
String Value retuned: I am a string

String passed(main reference) value: I am a string
Was it helpful?

Solution

Java passes a reference by value, this gives it the feel of being pass by reference despite actually being pass by value.

public class Main{

    public static void main(String[] args) {
        Vector3d vectorTest=new Vector3d(1,2,3);
        System.out.println(vectorTest.x); //prints 1
        affectVector(vectorTest);
        System.out.println(vectorTest.x); //prints 100
        replaceVector(vectorTest);
        System.out.println(vectorTest.x); //still prints 100
    }

    public static void affectVector(Vector3d vectorIn){
         vectorIn.x=100; 
    }

    public static void replaceVector(Vector3d vectorIn){
         //this method has no external effect because the reference vectorIn is immediately overrwritten
         vectorIn=new Vector3d(0,0,0); //the reference vectorIn is completely changed
    }


}

You can see that because a reference is passed by value you can still access the object that it refers to but if you replace the reference then you are 'refering' to a different object and that has no effect outside the method.

The analogy I use when trying to describe this is that of postal addresses. A reference is a postal address that you use to send letters (instructions) to objects. You can copy that address onto many pieces of paper but it still sends letters to the same house. It is the postal address that is copied, not the 'house'

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top