Pass by Reference in java example. Java supports only Pass by Value

            In Java, arguments are passed by value only because primitives are passed by value and object / array  references are also passed as values. Let us see in detail.  In Java all argument values that belong to one of basic types (primitives) are transferred to a method using pass by value mechanism. A copy is made for each argument value that is passed to a method. So any change in the argument in the method does not reflect in the variable of the calling function.

            As far as object is concerned, the value of the object is not passed to the method instead reference of the object is passed by value. So the contents of the object is not passed to method. This is an advantage  in java in terms of efficiency when passing arguments to a method which saves memory and time.

Similarly when we pass Array to the method, reference of the array variable is passed to the method.

Both in Object & Array, using the reference in the method, we can access the original object’s  field and change.  But assigning a new object’s reference to the original object does not change the original object’s value, because the original object’s reference is passed as a value not as address.  The following example illustrates.

Example :


package net.javaonline;

public class Ref

{
	 public static void main(String arg[])
		 {

		 StringBuffer g = new StringBuffer( "Hello" );

		 int m[]={0};
		 m[0]=10;

		 int i=10;
		 stcString stc=new stcString(); // creates object of the stcString class.
		 stc.setName("Kumar"); 

		 Integer j=new Integer(20);

		 test k = new test();
		 int value=k.change(i, j,m,stc,g); // reference of j,stc, g is passed to method .

		 System.out.println("i="+i + " j="+j + "m="+ m[0]+" value=" + value+ " Name="+stc.getName()+" g=" + g );
		 }
}
class test
{
     public int change(int a, Integer b, int c[], stcString stc,StringBuffer g )
       {
        stcString stc1=new stcString(); // stc, stc1 is the local object variable.
        stc1.setName("kamal");
        stc=stc1; // the value of the stc1 is not reflected in the original variable's (stc) name field in the calling function. This ensures that java passes the reference  as a value.  

        //stc.setName("Kannan"); // this statement changes the name of the original variable's name field in the calling function because using the reference we are accessing & modifying the field of the object.

         System.out.println("inside method g="+g); // prints original value
         g=new StringBuffer("Hello world"); // new object is created and the object reference is stored in the local variable g
         a=a+10;
         b=b+25;
         c[0]=a+b;
         return c[0];
 }
}

class stcString
   {
      String name="";
      public void setName(String k)
       {
        name=k;
        }
      public String getName()
        {
       return name;
   }

}

Output 1 :

inside method change g=Hello
i=10 j=20 m=65 value=65 Name=Kumar g=Hello

Output 2:

Comment the below  lines in the test class.

//stcString stc1=new stcString();
//stc1.setName(“kamal”); // stc, stc1 is the local object variable.
//stc=stc1; // the value of the stc1 is not reflected in the calling function

Uncomment the below line.

stc.setName(“Kannan”);

now run the program and the  output will be

inside method change g=Hello
i=10 j=20 m=65 value=65 Name=Kannan g=Hello

You may also like

Leave a Reply