Core JAVA

Java pass by reference or pass by value

Java is pass by value and not pass by reference i.e. Java copies and passes the reference by value, not the object. Thus, method manipulation will alter the objects, since the references point to the original objects. But if you change the reference inside method, original reference will not get change. If it was pass by reference, then it would have got changed also. Well, primitive types are always pass by value without any confusion. But, the concept should be understood in context of method parameter of custom types.Let’s discuss with one example as below.
public class Bar
{
    private String property;
 
    public Bar (String property){
        this.property = property;
    }
    public String getProperty() {
        return property;
    }
    public void setProperty(String property) {
        this.property = property;
    }
}
 
public class Main
{
     public static void main(String[] args){
          Bar bar = new Bar("bar");
          method1(bar); // here we are changing the instance that the reference variable "bar" refers
          method2(bar); // here we are not changing the reference!
     }
     public static void method1(Bar m1bar) {
          mbar.setProperty("modifybar");
   
     }
     public static void method2(Bar m2bar) {
         Bar newbar = new Bar("newbar");
         m2bar = newbar;
     }
}

In above example, method1() logical address bits of first instance(bar) are copied to another reference variable(m1bar), thus resulting both references to point a single memory location where actual object is stored. Remember, making another reference to null will not make first reference also null. But, in changing the property from either reference variable have impact seen in other reference also. But in method2() we are creating new reference “newbar” of same class i.e. new memory location of instance and after that change the value of the passing reference variable i.e m2bar assign to new memory location.

Let’s see step by step in java when we pass any reference of custom types as any method parameters always the memory address is copied to new reference variable. See in below picture:

1. Bar bar = new Bar(“bar”);
This statement will create an instance of class Bar, with “property” initialized to “bar”. The reference to this created instance is assigned to variable bar;

2. define public static void method1(Bar m1bar)
In this line of execution then a reference of type Bar with a name m1bar is declared and it’s initially assigned to null.

3. call of method1(m1bar)
This statement assign same memory location of object with both reference variables.


4. mbar.setProperty(“modifybar”);
This statement change the value property of instance and it is change for both reference variables.

5. in method2(m2bar) create new reference with other instance with property “newbar” as Bar

6. newbar = new Bar(“newbar”);


7. m2bar=newbar
Here, we have three reference variables bar, m2bar and newbar when statement executes, m2bar and newbar will point to same instance created inside the method. But bar is unchanged and it is continually pointing to instance, it was pointing originally.

Hope it help to clear you concept about pass by value in java.

Happy learning!!!.

 

Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

2 years ago