You are on page 1of 1

Home Java Programs OOPs

Write be(er
code

Open

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:

Sponsored

Luxury Villas Prices in Dubai Might


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.

Sponsored

Luxury Villas Prices in Dubai Might

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

public class Bike {  
        private double speed;  
        public void Model(){}  
        public Bike(double s){  
            this.speed=s;  
        }  
        public double getSpeed() {  
            return speed;  
        }  
        public void setSpeed(double s1) {  
            this.speed = speed;  
        }  
    }  

Now, create a TestSpeed class to swap variables:

TestSpeed.java

public class TestSpeed {  
    public static void main(String[] args) {  
            Bike apache = new Bike(180); //memory referen
            Bike pulsar = new Bike(200);  
              
            swap(apache, pulsar);  
            System.out.println("Apache's Speed is ="+apac
            System.out.println("Pulsar's Speed is ="+pulsa
            Demo(pulsar);  
            System.out.println("Pulsar's Speed is ="+pulsa
        }  
  
        private static void Demo(Bike b) {  
            b.setSpeed(180);  
            b = new Bike(280);  
            b.setSpeed(200);  
        }  
        public static void swap(Object o1, Object o2)// m
        {  
            Object temp = o1;  
            o1=o2;  
            o2=temp;  
        }  
    }  

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:

public static void swap(Object o1, Object o2)  
        {  
            Object temp = o1;  
            o1=o2;  
            o2=temp;  
        }  

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

public class PBVDemo {   
         int a=100;     
         void change(int a){    
         a=a+100;//Changing values  It will be locally)   
         }    
         public static void main(String args[]){    
           PBVDemo p=new PBVDemo();  //Creating objec
           System.out.println(" Value (before change)="+p
           p.change(500);  //Passing value  
           System.out.println(" Value (after change)="+p.a
         }    
        }  

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.

← Prev Next →

Youtube
For Videos Join Our Youtube
Channel: Join Now

Feedback

Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

Learn Latest Tutorials

Splunk tutorial SPSS tutorial

Splunk SPSS

Swagger tutorial T-SQL tutorial

Swagger Transact-SQL

Tumblr tutorial React tutorial

Tumblr ReactJS

Regex tutorial

Regex Reinforcement
Learning

RxJS tutorial

R Programming RxJS

React Native Python Design


Patterns

Python Pillow Python Turtle

Keras tutorial

Keras

Preparation

Aptitude

Aptitude Reasoning

Verbal Ability

Verbal Ability Interview


Questions

Company
Questions

Trending Technologies

AWS Tutorial

Artificial AWS
Intelligence

Selenium tutorial

Selenium Cloud Computing

Hadoop tutorial ReactJS Tutorial

Hadoop ReactJS

Data Science Angular 7

Git Tutorial

Blockchain Git

DevOps Tutorial

Machine Learning DevOps

B.Tech / MCA

DBMS tutorial

DBMS Data Structures

DAA tutorial

DAA Operating System

Computer Network Compiler Design

Computer Discrete
Organization Mathematics

Ethical Hacking Computer Graphics

html tutorial

Software Web Technology


Engineering

Automata Tutorial

Cyber Security Automata

C++ tutorial

C Programming C++

Java tutorial

Java .Net

Python tutorial List of Programs

Python Programs

Control System Data Mining

Data Warehouse

Luxury Villas Prices in Dubai


⇧ SCROLL TO TOPMight Surprise You
Sponsored Luxury Villas Dubai | Sponsored Li…

You might also like