HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Basics / Java Pass-by-Value vs. Pass-by-Reference

Java Pass-by-Value vs. Pass-by-Reference

There has been a good amount of debate on whether “java is pass by value or pass by reference?”. Well, lets conclude it last time, Java is pass by value and not pass by reference. If it had been pass by reference, we should have been able to C like swapping of objects, but we can’t do that in java. We know it already, right?

When you pass an instance to a method, its memory address are copied bit by bit to new reference variable, thus both pointing to same instance. 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.

To prove it, lets see how memory allocations happen in run time. It should solve the slightest doubts, if any. I am using following program for demonstration of the concept.

public class Foo
{
	private String attribute;

	public Foo (String a){
		this.attribute = a;
	}
	public String getAttribute() {
		return attribute;
	}
	public void setAttribute(String attribute) {
		this.attribute = attribute;
	}
}

public class Main
{
     public static void main(String[] args){
          Foo f = new Foo("f");
          changeReference(f); // It won't change the reference!
          modifyReference(f); // It will change the object that the reference variable "f" refers to!
     }
     public static void changeReference(Foo a) {
          Foo b = new Foo("b");
          a = b;
     }
     public static void modifyReference(Foo c) {
          c.setAttribute("c");
     }
}

Lets see what happen on runtime step by step :

1) Foo f = new Foo(“f”);

This statement will create an instance of class Foo, with ‘attribute’ initialized to ‘f’. The reference to this created instance is assigned to variable f;

1-8158113

2) public static void changeReference(Foo a)

When this executes then a reference of type Foo with a name a is declared and it’s initially assigned to null.

2-7952427

3) changeReference(f);

As you call the method changeReference, the reference a will be assigned to the object which is passed as an argument.

3-7934433

4) Foo b = new Foo(“b”); inside first method

This will do exactly the same as in first step, and will create a new instance of Foo, and assign it to b;

4-7475542

5) a = b;

This is the important point. Here, we have three reference variables and when statement executes, a and b will point to same instance created inside the method. Note: f is unchanged and it is continually pointing to instance, it was pointing originally. NO CHANGE !!

5-7576774

6) modifyReference(Foo c);

Now when this statement executed a reference, c is created and assigned to the object with attribute “f”.

6-9650208

7) c.setAttribute(“c”);

This will change the attribute of the object that reference c points to it, and its same object that reference f points to it.

8-1270952

I hope that this explanation was enough clear to make your understanding better, if it was not already.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Anand Shinde

    January 24, 2020

    Foo b = new Foo(“b”); Instead of this we can do a = new Foo(“b”); then what happen

    • Vijay

      February 25, 2020

      The reference b won’t be created and the instance object is assigned to a reference.

  2. Ed

    October 15, 2019

    In step 5, the value is actually changed within the scope of the changeReference() function.
    If you were to print out the value inside the scope of the function, the value will be “b”.
    This is because the variable a only has local scope since it is declared in the signature of the function. At the end of the function, a goes out of scope, unhiding/unshadowing the original variable.

    While this example is correct, I think the confusion with Java isn’t PassByValue vs. PassByReference. Its PassByReference vs. Using Objects By Reference. Java doesn’t pass values as arguments by reference, however it does very much .use objects by reference for performance optimization reasons.

    • Jeffrey Serio

      January 28, 2020

      This is correct. Here is what happens:

      Original memory address of f: Foo@38cccef
      Original value of f: f

      Inside changeRef scope, before change:
      Memory address = Foo@38cccef
      Value = f

      Inside changeRef scope, after change:
      Memory address = Foo@64b8f8f4
      Value = b

      Inside modifyRef scope, before change:
      Memory address = Foo@38cccef
      Value = f

      Inside modifyRef scope, after change:
      Memory address = Foo@38cccef
      Value = c

  3. Yogesh Bombe

    September 4, 2019

    Very good explanation

    I have confusion in the point 2 line “initially assigned to null”

    But when i print the reference before assignment initially it point the reference of f.

    public static void changeReference(PassByValue a) {
    		System.out.println("Before Reference of a "+a);
    		PassByValue b = new PassByValue("b");
    		a = b;
    		System.out.println("Reference of a "+a);
    		System.out.println("Reference of b "+b);
    	}
    

    Please correct me if i am wrong.

    • Vijay

      February 25, 2020

      It will not be assigned to NULL. Actually a reference is created and the instance is assigned to it and passed to the method.

      Foo a = f; — this will happen behind and passed the reference to the method

  4. Sumit Chouhan

    September 10, 2017

    nice !
    it is very easy to understand.
    Thnaks

  5. Ancil Hameed

    July 6, 2017

    Very interesting and easy to understand!! Keep up the good work Lokesh 🙂

  6. Anurag

    July 27, 2016

    clear demonstration… well done..

  7. hkv

    June 17, 2016

    Very helpful and easy to understand the complex topic. Thanks

  8. Arvind Verma

    February 10, 2016

    great !!!!

  9. Hemant

    March 3, 2014

    great work

  10. Umamahesh REDDY

    September 25, 2013

    Nice explanation..thanks

Comments are closed on this article!

Search Tutorials

Java Basics

  • Java Introduction
  • Java Installation
  • Java Hello World
  • Java JDK, JRE and JVM
  • Java Classes and Objects
  • Java ClassPath
  • Java Operators
  • Java Data Types
  • Java Primitive Types
  • Java Variables
  • Java Comments
  • Java main() Method
  • Java Command Line Args
  • Java Wrapper Classes
  • Java Types of Statements
  • Java Block Statement
  • Java Pass-by-Value
  • Java System Properties
  • Java Static Import
  • Java hashCode() and equals()
  • Java Immutable Class
  • Java 32-bit vs 64-bit
  • Java java.exe vs javaw.exe
  • Java Generate Bytecode
  • Java Naming Conventions
  • Java Little-Endian vs Big-Endian

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)