Creating copies of values inside the variables and passing the method as arguments is called as “passing by value”. In “pass by value” the actual variable will not be passed. In Java, all arrays, primitive and class types are passed by value. “Pass by reference” is not used in Java.
Consider the following code as an example for pass by value
public void Indi(int var)
In the above code, we have a method named “Receiving” which contains a variable “19”. Now let us add some code which calls the method “Indi”.
public static void main(String  args)
int NewState1= 1;
System.out.println(“The value NewState1 is: ” +NewState1);
In the above code, the main method is passed with the variable NewState1 which has a value “1”. When the value of the variable is passed inside the method “Indi”, it will add both the variables. If you think that you will be getting a output of 20, then you are wrong. Are you wondering why?
SEE: Java interview Q&A available here
The output will be “1” because java is passed by value so only the copy of the variable “1” is passed into the method India. The original variable still remain in main() method which is printed as output.
Why passing in Java is called as pass by value?
According to our example code, a copy of variable NewState1 with value “1” is created and passed into the method “India”. The value of variable in main method is passed into the method “India” while its reference is not allowed. Since only the value is allowed pass into another method it is called as “pass by value”.
Why objects are not passed by reference in Java?
It is now clear for us that pass by reference is not used in Java. If objects are passed by reference, the memory where the object is stored is passed instead of its value. While passing by reference object copies are not created so an object will be shared between the methods. During pass by reference changes made on objects in one method will reflect in another which prevents sharing of objects between methods. Writing a Java program without sharing objects between methods is complicated hence pass by reference is not used in Java.
Swap test method can be used to check whether a program is passed by value or passed by reference
In swap test method two arguments are swapped inside a method. If arguments are really swapped and changed its value then the program is passed by reference else it is passed by value.
Consider the following code as an example
public class Swp1
public static void main(String args)
Anima a1 = new Anima(“Dog”);
Anima a2= new Anima(“Cow”);
System.out.println(“Perior to swap: a1:”+a1 + “; a2:” +a2);
System.out.println(“After the swap process: a1:”+ a1 + “; a2:” + a2);
public static void swap(Anima anima1, Anima anima2)
Anima tmp1= new Anima(“”);
public Anima(String nam1)
public String toString()
The output for the above program is
Before Swap: a1:Dog; a2:Cow
After Swap:- a1:Dog; a2:Cow
In the above program, two arguments namely a1 and a2 are passed in the main method and swapped. Since the program is written in Java which passes by value, the arguments passed inside the main method remains same.
- Download: Final year projects for free
How arrays used in Java are passed?
Most novice Java programmers believe that arrays used in java are passed by reference because they think that arrays are not objects. In Java, everything including arrays are considered as objects and are passed by value only. You may think that when an array is passed between methods its contents may change because same address is used for both arrays. Unlike other objects arrays are not passed between method in java instead the reference address of arrays is passed. Since only reference address is passed the contents of array remains same even when it is passed by value.