Java is one of the most fundamental programming languages that anyone can pick up. It is so simple at the same time, the possibility of what you can develop using Java, is infinite! This is what makes Java the most widely used language in the industry today. This blog will help you understand about the pass by value and the pass by reference in Java in the following sequence:
Pass by Value and Pass by Reference in Java
Pass by Value and Pass by reference is the two ways by which we can pass a value to the variable in a function.
Pass by Value: It is a process in which the function parameter values are copied to another variable and instead this object copied is passed. This is known as call by Value.
Pass by Reference: It is a process in which the actual copy of reference is passed to the function. This is called by Reference.
Talking about Java, we can say that Java is Pass by Value and not pass by reference.
We will illustrate the up-quoted statement by programming.
Illustration with Program
Let’s say we have a class Car like below.
public class Car { private String color; public Car () {} public Car (String a) { this.color=a; } public String getColor() { return color; public void setColor (String color) { this.color = color; }
Next we have a program which swaps two objects.
public class Disp { public static void main(String[] args) { Car red = new Car("Red"); Balloon yellow = new Balloon("Yellow"); swap(red, yellow); System.out.println("red color="+red.getColor()); System.out.println("yellow color="+yellow.getColor()); desc(yellow); System.out.println("yellow color="+yellow.getColor()); } private static void desc(Car car) { //car=100 balloon.setColor("Red"); //car=100 car= new car("Green"); //car=200 car.setColor("Yellow"); //car = 200 } // swap method public static void swap(Object o1, Object o2){ Object temp = o1; o1=o2; o2=temp; } }
Output
Red color=Red
Yellow color=Yellow
Yellow color=Red
Explanation:
So as I told earlier that JAVA is pass by Value, therefore, we can see that the first two lines in the output the swap () method didn’t work. Let’s do a thorough explanation of the code.
First, we created an instance of a class by using the new operator. It is created and the value is stored at the memory location. Therefore this instance is pointing the memory location of objects that is “red” and “yellow”. Now when we are calling the values with new objects o1 and o2 then it is pointing to the memory location of the two objects that is “red” and “yellow”. So when the swap method gets invoked at first it points to the memory location of “Red”. In the next line, Car reference is changed to the next address and it gets pointed to the memory location of “yellow”. Therefore they get swapped at the second step, not on the first step. This is why JAVA is referred to as Pass by Value and not reference.
Now that you have understood Pass by Value and Pass by Reference in Java, check out the Java Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA Training and Certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this “Pass by Value and Pass by Reference in Java” article and we will get back to you as soon as possible.