Call by value and Call by reference Java Tutorial

Call by value and Call by reference Java Tutorial


Does Call by reference Java supports?

This is a very basic question asked in every interview and should known to each programmer also.

Everyone says "no" as Java does not support pointers. No one actually see the address of the object in Java. No one can dream of call by reference without pointers with their experience of C/C++.

Of course, they are very correct.

In Java the method parameters can be primitive data types or object references. Both are passed as value only but small tricky explanation is here for object references. I repeat, when primitive data types are passed as method parameters, they are passed by value (a copy of the value) but incase of object references, the reference is copied (of course, here also a copy only) and passed to the called method. That is, object reference is passed as a value. So, original reference and the parameter copy both will refer the same Java object. As both refer the same object, if the calling method changes the value of its reference (passed from calling method), the original object value itself changes. Note that object itself is not passed, but it’s references is passed.

Object reference is a handle to a Java object. Do not compare this reference term with the used in C/C++. In C/C++, the reference directly points to the memory address of a variable and can be used for pointer manipulations.

Finally to say, in Java, everyone is passed by value. But with objecct, the value of the reference is passed.

Let us see two programs on call by value and call by reference.

Case 1: Call-by-value or Pass-by-value

In the following program, a data type int is passed as parameter to a method call.

The variable x value 10 is passed to parameter of y of display() method. As copy of x is passed, changing y does not change x value in main() method.

Case 2: Call by reference Java or Pass-by-reference

Here, for display() method the reference of StringBuffer object is passed.

The value of object reference sb1 is passed to sb2. sb1 literal value "Hello" is passed to sb2. Now sb1 reference and sb2 reference refer the same object. Changing sb2 affects sb1 also. Both sb1 and sb2 prints "HelloWorld".

View All for Java Differences on 90 Topics

6 thoughts on “Call by value and Call by reference Java Tutorial

  1. Shivshankar Nagarsoge

    What do you think about this program? Is it call by value or reference as per your post?
    class Test {
    public static void swap(Integer i, Integer j) {
    Integer temp = new Integer(i);
    i = j;
    j = temp;
    }
    public static void main(String[] args) {
    Integer i = new Integer(10);
    Integer j = new Integer(20);
    swap(i, j);
    System.out.println(“i = ” + i + “, j = ” + j);
    }
    }

  2. aditya gupta

    public class Test
    {
    int x=12, y=25;

    public static void main()
    {
    int x=5, z=10;
    String s= “Java is Fun”;
    System.out.println(x+” “+ y + “ “ + z);
    Function1(x,z);
    System.out.println(x+ “ “+y + “ “+z);
    System.out.println(s);
    Function2(s);
    System.out.println(s);
    }// end of main

    public static void Function2( String s)
    {
    System.out.println(x+” “+ y );
    s=”Java is Platform Independent”;
    System.out.println(s);
    return;
    } // end of Function2

    public static void Function1(int x, int z)
    {
    System.out.println(x+ “ “+y + “ “+z);
    x++; z++; y++;
    System.out.println(x+ “ “+y + “ “+z);
    return;
    } // end of Function1
    }// end of class

    1. Identify the calling and the called methods.

    2. Which function is called using CBV and which is called using CBR?

    3. Identify the local variables in each method.

    4. Identify the global variables.

    5. Identify the composite datatypes in the above code

    6. Give the output of the above code

  3. Pankaj

    class CallByReference
    {
    public void display(StringBuffer sb2)
    {
    sb2.append(“World”);
    System.out.println(sb2); // prints HelloWorld
    }
    public void setData(Integer i) {
    ++i;
    }
    public static void main(String args[])
    {
    CallByReference cbr = new CallByReference();
    StringBuffer sb1 = new StringBuffer(“Hello”);
    cbr.display(sb1);
    System.out.println(sb1); // prints HelloWorld
    Integer a = new Integer(19);
    cbr.setData(a); //It should append the value of a
    System.out.println(a); //It should print 20 as per the code which you explained but its giving 19
    }
    }

Leave a Reply

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