Different Ways to Achieve Pass By Reference in Java
There are two types of parameters one is Formal parameters and the second is Actual Parameters. Formal parameters are those parameters that are defined during function definition and Actual parameters are those which are passed during the function call in other Function.
Showcasing the formal and actual parameters through code:
- Functions can be differentiated on the basis of parameters.
- And there are two ways to pass the parameters in functions: call by value and call by reference.
- C/C++ supports the call by reference because in the call by reference we pass the address of actual parameters in the place of formal parameters using Pointers.
- And Java does not support Pointers that’s why Java does not support Call by Reference but c/c++ support pointers hence these language support call by Reference.
Swapping of Two numbers in C/C++ language is done by the call by Reference approach
Below is the implementation of Call by Reference in C
Value of a 15 Value of b 5 Value of a 5 Value of b 15
In java, if we try to swap numbers in java the changes in the state of the two numbers is valid only in the particular swapping method
Swap two numbers in java:
Value of the a 5 Value of the b 10 Value of a in swap function 10 Value of b in swap function 5 Value of the a 5 Value of the b 10
There are some ways to achieve pass by reference in java in the place of the call by reference:
1. Make a particular variable of a particular datatype as a class member
- A class member can be accessed throughout the class definition and we can access members using this keyword when there is a local variable that has the same name as the class member’s name present in the particular function body or function definition in order to avoid confusion.
- In order to pass the reference, we pass the object of the class in the place of the actual parameter and the formal parameter of a class object type has the same reference to each other that’s why with the help of the formal parameter object of class any changes will be reflected in both objects formal and actual objects.
Number value 0 Updated Number value 1
2. Passing collections or Single element array as parameter:
- We can pass collections like ArrayList, stack, etc or Single element array as actual parameters, and both the actual and formal parameters of the function have the same reference to the memory address.
Number Value 0 Number Value 1 List [1, 2] Updated List [2, 3]
3. Update the Return value:
- Naive Approach to perform reference by simply updating the return value with the previous value.
Number 3 Updated number 4
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.