Pass by value or pass by reference?

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Hello all, there is a lot of confusion about passing of parameters to methods in java. Are they passed by value or passed by reference? A short answer is , Java always uses “pass by value”, even of the objects.

Let us write a program and understand what this is all about.

This program outputs,

What did we do?

  • Created a method named modify(int i)which takes an int argument .
  • Created an overloaded version modify(Dimension dim) that takes an object reference as an argument.
  • Created public static void main(String[] args) so that we can execute the program and call these methods from it.

now consider this piece of code

  • Variable  i  is initialized to 10
  • We passed the variable i to modify method where all the action goes
    • Here, a copy of value is passed to the value and not the actual variable i.
    • This new value is being referred into the method modify using its parameter i.
    • In method, parameter i is incremented by 1 and its value is printed in the method itself. So 10 + 1 = 11 and it is printed.
    • Now, after method has completed executing, control is back to the calling method where we are again printing variable i. Now this time it prints 10 because this is not the same variable as referenced in the method. There was a copy of the variable used in the method, not the original one.
  • From this example, we came to know that primitive value is passed by value.

 

This is how pass by value works for primitives

This is how pass by value works for primitives

Now consider following piece of code

  • We created a new Dimension(5,10) with height as 10
  • Passed the object to modifymethod
    • Note that in modifymethod, the object referred by variable d outside the method is now being referred in the method using variable dim.
    • This means that there are two variables d and dim referring to the same object that was previously being referred by only d outside the method.
    • Inside modify method, we are changing the value of height using dim.height = dim.height + 1;
    • Inside the method, it prints 11
    • Outside the method also it prints 11.

    Why is this difference? Why is it that the value of primitive is unchanged outside the method but it is changed for a reference variable?

  • This is because in case of reference variable, even if there are two reference variables, they are referring to the same object .
  • Only difference is that the Dimension object was being referred by two different reference variables. Changes done by one variable will remain because ultimately they are modifying the same object.

 

How pass by value works for object references

How pass by value works for object references

 

Thus we can come to the conclusion that for primitives, a copy of value is made and for reference variables, a copy of reference variable is made when passing to a method and not the object itself.

 

 

 

Share Button

10 comments for “Pass by value or pass by reference?

  1. preeti
    September 6, 2013 at 11:49 pm

    So when we pass an object does it imply that we are passing address of the memory location of that object? Which is copied by ‘dim’ in this case. Is that why they refer to the same object? Please correct me if i’m wrong.

    • Prasad Kharkar
      September 7, 2013 at 9:15 am

      So when we pass an object does it imply that we are passing address of the memory location of that object?

      It means that a copy of reference variable will be created which contains the address of the object. By passing variable d to modify(Dimension dim), we are actually referring the same object by two variables i.e. d and dim and dim contains the content of the variable d. i.e. the address of the object is copied into dim.
      Does this answer your question? Happy learning 🙂

      • preeti
        September 7, 2013 at 10:15 am

        Yes.. Thank you..

  2. Sudhir Kumar
    November 21, 2013 at 12:09 am

    Thank you sir. but why we called it ,

    This is because in case of reference variable, even if there are two reference variables,
    they are referring to the same object .
    Only difference is that the Dimension object was being referred by two different reference
    variables. Changes done by one variable will remain because ultimately they are modifying
    the same object.

    Java always uses “pass by value”, even of the objects.

    • Prasad Kharkar
      November 21, 2013 at 10:48 am

      Sudhir, I did not understand what exactly you wanted to say in the comment. What I want to elaborate is java always uses pass by value even for the object. The values in reference variables is copied.

  3. Sudhir Kumar
    November 23, 2013 at 1:02 am

    hhm… understood,pass by value. my question wrong , i was hurry .

  4. navin
    January 18, 2015 at 10:50 pm

    Hello Prasad Kharkar,
    Wonderful article.
    My confusion in this is that while we pass int or string type variables it works as Pass by value.
    But the same dont work for array, Linked List.. Why so ??
    There it works as pass by reference.

    • January 19, 2015 at 10:20 am

      . Hi Navin, I think you will have to read the article again 🙂 For primitives, a copy of value is made and for reference variables, a copy of reference variable is made when passing to a method and not the object itself.
      In short, it is always pass by value in java

      • Navin
        January 19, 2015 at 11:08 pm

        But in that case also the values changes right.
        Check this:

        public class Test {
        public static void main(String[] args) {
        int[] a = { 1 };
        increment(a);
        System.out.println(a[0]);
        }

        static void increment(int[] i) {
        i[0]++;
        }
        }

        Here the value in array i is getting changed even though we are not reassigning back the array. But the value is getting changed why so ?

        • January 20, 2015 at 2:30 pm

          Let us see it this way. When you declare ‘int [] a = {1}’ you are creating an array object which is being referred by reference variable ‘a’. Now you are passing it to method ‘increment(int [] i)’. Now both ‘a’ and ‘i’ are referring to the same array object. By pass by value, I mean the address location of array object that is stored in variable ‘a’ is copied into ‘i’. So ‘a’ and ‘i’ are both referring to same array object. There are no two different objects. Only only array object that is being referred by two different reference variables.

Leave a Reply

Your email address will not be published. Required fields are marked *