Methods can be called in two ways, call by value and call by reference. Call by value means we call a method by passing value in a parameter. Call by reference means we call a method by passing reference (location of the variable) in a parameter. In Java, primitive data types (Int, float, double, Boolean, char, etc.) are always passed as values and Non-Primitive data types (class, object, array, string, and interface) are always passed as a reference. In this article, we will learn more about call by value and call by reference in java.
Call by Value in Java:
In call by value, we call a method by passing the value in the parameter. In call by value copy of the variable is passed to the method so all the changes are reflected only in that method hence no changes will be reflected in the main method this is the main difference between call by value and call by reference in java. When we pass any variable with primitive data types, it will be considered as call by values, so the variable’s modification will not reflect in the caller’s scope.
Example of Call by value:
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
In the above example, we created a method increment with an int data type parameter number. In this method, we increment the value of the variable number by 1. In the main method, we created a variable number with an int data type and assign a value 10 to the variable. Now we called method increment and passed the variable number to the method. We print the value of the number before calling the method, in the method increment, and after the method call is over. In the output, we can see that the value of the variable only changed (value = 11) in the method increment before the method call and after the method call value 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 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 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 object pb of the class Prepbytes. Now we passed the object of the class to the method call. Now, if we increment the value of the variable number it will also reflect in the main method. 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 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 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:
1. Java is a call by value or a call by reference?
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 pass to the method so changes will not reflect in the objects.
2. How to achieve call by reference in java?
We can use non-primitive data types like object, class, string, array, and interface to achieve call by reference in java. We have already seen how we can use these non-primitive data types to achieve call by reference with help of an example.
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