Javatpoint Logo
Javatpoint Logo

Java Pass by Value

People usually take the pass by value and pass by reference terms together. It is really confusing and overhear questions in interviews, Is java pass by value or passes by reference, or both? So the answer to this question is Java is strictly pass by value. There is no pass by reference in Java.

Let's understand what some of the different mechanisms for passing parameters to functions are:

  • value
  • reference
  • result
  • value-result
  • name

Nowadays, the two most used and common mechanisms are pass by value and pass by reference. Let's discuss them:

Pass by Value: In the pass by value concept, the method is called by passing a value. So, it is called pass by value. It does not affect the original parameter.

Pass by Reference: In the pass by reference concept, the method is called using an alias or reference of the actual parameter. So, it is called pass by reference. It forwards the unique identifier of the object to the method. If we made changes to the parameter's instance member, it would affect the original value.

Java does not support pass by reference concept.

About the Parameters Passed in Java

The fundamental concept for passing the parameters in modern programming languages is passing by value and passing by reference. But, in Java, the pass by reference concept is degraded. It supports only the pass by value concept.

The primitive variables hold the actual values, whereas the non-primitive variables hold the reference variable. However, both variables use stack memory to store the values. See more about data types in Java.

In Java, during the method invokation, a copy of each argument is created then passed to the method.

In the case of primitive data types, it copies the value inside stack memory then pass it to the method. In the case of non-primitive data types, it points a reference in stack memory to actual data, which occurs in a heap. When we pass an object, it will copy a reference from the stack memory and pass it to the callee method.

Let's demonstrate it with some examples:

Create a Bike class having objects and methods.

Bike.java

Now, create a TestSpeed class to swap variables:

TestSpeed.java

Output:

Apache's Speed is =180.0
Pulsar's Speed is =200.0
Pulsar's Speed is =200.0

From the above output, we can see that the swap method did not work. It did not work because Java is pass by value, and, here, we are passing the reference of the object. So it is clear that Java does not support pass by reference.

Explanation: In the above program, when we create an instance of the class Bike using the new operator, the instance of the class is created, and the variable holds the reference of the memory where the object is saved.

While calling the swap() method, we have created two new variables o1 and o2, which are pointing to the memory location of the apache and pulsar variable. Below is the swap method implementation in the above program:

As we can see from the above code snippet, the values of o1 and o2 are changed. They are copies of the apache and pulsar reference locations. So, it did not change the values of apache and pulsar in the output.

Java Pass by Value Example

Passing the parameters by values does not affect the original variable. Below is the example of Passing by Value:

PBVDemo.java

Output:

 Value (before change)= 100
 Value (after change)= 100

As we can see from the above output, the original values is not affected by the pass by value mechanism.

Also, see Call by value & Call by Reference in Java.






Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA