Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Call by Value and Call by Reference in Java

Last Updated on May 9, 2023 by Prepbytes

Methods can be accessed in two ways: by value and by reference. Calling a method by value means passing a value as a parameter. Calling a method by reference means passing a reference (the variable’s location) as a parameter. Primitive data types (int, float, double, boolean, char, and so on) are always passed as values in Java, while non-primitive data types (class, object, array, string, and interface) are always passed as references. This article will go over call by value in Java and call by reference in Java.

Call by Value in Java

In Java, call by value refers to calling a method by passing the value in the parameter. In Java, call by value passes a copy of the variable to the method, so all changes are reflected only in that method; thus, no changes are reflected in the main method. This is the main difference between call by value in java and call by reference in java. When we pass a variable with primitive data types, it is considered a call by values in Java, so any changes to the variable will not be reflected in the caller’s scope.

Example of Call by Value in Java

class Prepbytes{
    public static void increment(int number){
        number = number+1;  // increment variable by 1
        System.out.println("value in method: "+number);
    }
    
    public static void main(String[] args) {
        int number=10;
        System.out.println("value before method call : "+number);
        increment(number);
        System.out.println("value after method call: "+number);
    }
}

Output

value before method call: 10
value in method: 11
value after method call: 10

We created a method increment with an int data type parameter number in the preceding example. In this method, we increase the variable number’s value by one. We created a variable number with an int data type and assigned it the value 10 in the main method. Now we called method increment and passed the variable number to it. We print the value of the number before calling the method, during the method increment, and after the method call is completed. The value of the variable only changed (value = 11) in the method increment before the method call, and the value after the method call is the same (value = 10). This is how call by value works in Java.

Call by Reference in Java

Though Java is strictly call by value when we pass the reference of the object it creates a copy of the reference and then passes it as value to the method. The copy reference also points to the same address so all the changes also reflect in the main method this is the main difference between call by value in java and call by reference in java. Let’s see examples of how we can achieve call by reference in java.

Example of Call by Reference in Java using Object

class Prepbytes{
    int number=10;
    
    // pass object as parameter
    public static void increment(Prepbytes pb){
        pb.number = pb.number+1;  // increment variable by 1
        System.out.println("value in method: "+pb.number);
    }
    
    public static void main(String[] args) {
     Prepbytes pb=new Prepbytes(); // pb is an object of class Prepbytes
        
        System.out.println("value before method call: "+pb.number);
        increment(pb); // pass object of the class prepbytes
        System.out.println("value after method call: "+pb.number);
    }
}

Output

value before method call: 10
value in method: 11
value after method call: 11

In this example, we created an object pb of the class Prepbytes. We’ve now passed the class object to the method call. If we increase the value of the variable number, the main method will also change. So now if we print the value of the variable number it will be modified (value=11) after the increment method call. This is one way to achieve call by reference in java.

Example of Call by Reference in Java using Array

class Prepbytes{
    
    // pass array of type int as a parameter
    public static void increment(int[] number){
        number[0] = number[0]+1;  // increment first element of an array by 1
        System.out.println("value in method: "+number[0]);
    }
    
    
    public static void main(String[] args) {
        int[] number=new int[1]; // create an array of size 1
        number[0]=10; // assign value to fist element of an array
        System.out.println("value before method call: "+number[0]);
        increment(number);
        System.out.println("value after method call: "+number[0]);
        
    }
}

Output

value before method call: 10
value in method: 11
value after method call: 11

In this example, we created an array of size 1 and data type int then we assigned value 10 to the first element of the array (index 0). Then we passed the array to the method call and increment the first element of the array by 1 so now the value becomes 11 this modified value will also reflect in the main method because when we pass an array as a parameter first copy of the reference is created then it is passed in the method call so modification also reflects in the main method. In the output, we can see that value of the first element of an array is changed after the method call. This way using an array we can achieve call by reference in java.

Example of Call by Reference in Java using String

class HelloWorld {
    
    // pass an object of StringBuilder as a parameter
    public static void change(StringBuilder s){
        s.append(" world!"); // add word in s
        System.out.println("value in method: "+s.toString());
    }
    
    
    public static void main(String[] args) {
        StringBuilder s=new StringBuilder("Hello");
        
        System.out.println("value before method call: "+s.toString());
        change(s);
        System.out.println("value after method call: "+s.toString());
        
    }
}

Output

value before method call: Hello
value in method: Hello world!
value after method call: Hello world!

In the above example, We created the string using StringBuilder and assign the initial value as ‘Hello’. Now we called method change and passed the object of StringBuilder s as a parameter. In the change method, we append the word ‘ World!’ to the string s as we passed the object of StringBuilder any modification to object s will reflect in the main method. In the output, we can see that before calling the change method value of s was ‘Hello’ and after calling the change method value of s becomes ‘Hello World!’.

FAQs Related to Call by Value or Call by Reference

Q1. Java is a call by value or a call by reference?
Ans. Java is always call by value it is not call by reference. We have already seen how call by reference works in java. Now let’s understand how java is always call by reference with help of an example.

Example:-

class Square{
    int length;
}

public class Main
{
    public static void main(String[] args) {
        Square square1=new Square();
        square1.length=10;

        Square square2=new Square();
        square2.length=5;

        System.out.println("Square1 length before: "+square1.length);
        System.out.println("Square2 length before: "+square2.length);

        swap(square1,square2);

        System.out.println("Square1 length after: "+square1.length);
        System.out.println("Square2 length after: "+square2.length);
    }

    public static void swap(Square square1,Square square2){
        Square tmp=square1;
        square1=square2;
        square2=tmp;
    }
}

Output:-
Square1 length before: 10
Square2 length before: 5
Square1 length after: 10
Square2 length after: 5

In the above example, we have created two objects of class Square (square1, square2 ) and set values of both objects 10 and 5 respectively. Now we will pass both objects in the swap method. In the swap method, we will swap both objects and we will print the values of both objects in the output. We can see that the values of both objects are still the same. It is because when we pass an object in the method java creates a copy of the object and passes it to the method so changes will not reflect in the objects.

Q2. How to achieve call by reference in java?
Ans. To perform call by reference in Java, we can use non-primitive data types such as object, class, string, array, and interface. With the help of an example, we have previously shown how we may use these non-primitive data types to achieve call by reference in Java.

Q3. Why is there no call by reference in Java?
Ans. Java does not support call by reference because we need to supply the address and addresses are kept in pointers. Java does not support pointers since they violate security.

Q4. Is call by reference allowed in Java?
Ans. Call-by-reference or pass-by-reference is not supported in Java.

Q5. Is Arraylist passed as a call by reference in Java?
Ans. A List supplied to a method is passed by reference. Objects added to the List within the method will remain in the List when the method returns.

For more, You can refer these articles:
1. Hello world program in Java
2. Recursion program in Java
3. String programs in Java
4. Multiple inheritance in Java

Leave a Reply

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