Updated June 3, 2023
Introduction to Java Call By Reference
The definition of the call by reference in Java infers that we use parameters as a reference. We send the argument reference as a parameter to the function which will use this. We ship both arguments as actual and formal parameters. These will refer to the exact location. That means whenever changes are made in a particular function, then these changes will be reflected in the actual parameters when the process is being called. There is no need for specifying or calling it explicitly. The address of the variables is sent, which means that these actual variables are calling the function.
Syntax
Below is a way in which you can implement call by reference in Java:
void function_name(datatype1, datatype2); //Function declaration
int main()
{
Datatype declaration;
Function Call(datatype1, datatype2);
}
Function_name ()
{
// Function definition
}
Explanation: The above syntax is first declaring a function. Unlike C, in Java, there is no specific provision for taking the variable at a particular address. Java uses call by value which passes the reference variables as well.
How Call by Reference work in Java?
The basic functionality of Call by reference is that we refer to the address of the variable. And hence if there is any change at the address of the variable, it will be reflected wherever the function using it is called. The below code will help you understand this functionality better.
Code:
Void inc(int );
Public static voidmain(string args[]) {
int a = 10;
System.out.println("Before function was called the Value of a = ", a);
void inc();
System.out.println("After function is called Value of a = ", a);
}
Void inc(intx) {
int x = x + 1;
}
Here we have declared the function inc with a variable. After that, we declared a variable. The variable will take the value which is as defined. But as it goes ahead, we call the ‘inc’ function. The function ‘inc’ will be called, and whenever it is called, it will point to the location of the variable ‘a’. It will create a copy of the reference variable, and once there are values, these will be passed to the methods used. This reference points to the address of our variable ‘a’. It will increment this value, and then when the function is called, it takes the value from this address. It will be 11 in this case as the inc function is increasing the value by 1. The value will be printed in the main. We can also create a copy of the reference variable. If a new object or variable is created for reference, it will not be affected as the primary address remains the same.
Examples to Implement Java Call by Reference
We can check a few more examples on this, which will help us understand better:
Example #1
Swapping numbers in Java by using Call by Reference. The below code is an example in which we can swap two numbers by using call by reference:
Code:
public class EduByReference {
public static void main(String[] args) {
int num1 = 55;
int num2 = 85;
System.out.println("The values before swapping are: number1 = " + num1 + " and number2 = " + num2);
// Calling the function to swap
swapByReference(num1, num2);
System.out.println("\nThe numbers before and after swapping will be same in main:");
System.out.println("Once the swapping is done, the values will be back to original: num1 = " + num1 + " and num2 is " + num2);
}
public static void swapByReference(int num1, int num2) {
System.out.println("Before swapping in function locally, num1 = " + num1 + " num2 = " + num2);
// Swapping number1 and number2
int temp = num1;
num1 = num2;
num2 = temp;
System.out.println("After swapping in function locally, num1 = " + num1 + " num2 = " + num2);
}
}
Output:
Explanation: The above program is swapping two numbers. We have taken two integer variables, a and b, with values 55 and 85, respectively. We then call the function swapByReference(a, b). Here in this function, we get these variables sent from the main. The function takes the values of a and b and then swaps the values of these two variables. For swapping, we have used another variable, ‘c.’ This function will return the value of 85 and 55 once this is done. We see that in main the values remain the same as before. This is the use of a call-by-reference. Once the function is called, the original value is stored as it is, and the values are changed for the function. The original values remain intact. The output of the above function will be as below.
You can see once the work by function is done, the original values are retained because of the address of the variable. The variables did not change their position.
Example #2
Adding two numbers by using call by reference in Java:
Code:
public class EduAddByReference{
int d=50;
public void add(int d){
d=d+100;//These changes will happen locally, that means it will be reflected only in this function
System.out.println("The value in function after performing addition is " + d);
}
public static void main(String args[]){
EduAddByReference edad=new EduAddByReference();
System.out.println("before change "+edad.d);
edad.add(500);
System.out.println("after change "+edad.d);
}
}
Output:
Explanation: The above program is another example of a call-by reference in Java. Here in the the main, we have called the function EduAddByReference(). This function takes the number from the main and adds it. This edition is being printed in the function. This value for the variable will remain local to this function. Once the control of the function goes to the main,, it prints the variable again,, which will have the original value. You can check the below output,, which will help us get this program working better.
Conclusion
The Call by reference in Java calls the functions and uses the address of an operand or variable to check the data. The value changes locally and is persistent only until the function’s scope. The Call by reference helps keep the original values intact and modify them whenever needed. It can be used whenever you want a variable to perform some operation locally. It retains the originality of a variable.
Recommended Articles
This is a guide to Java Call by Reference. Here we discuss an introduction to Java Call by Reference, and how it works with programming examples for better understanding. You can also go through our other related articles to learn more –