HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions

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

By Lokesh Gupta | Filed Under: Java Basics

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;

innstance creation

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.

Null reference

3) changeReference(f);

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

reference assignment

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;

new instance

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 !!

assignment

6) modifyReference(Foo c);

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

new reference

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.

modify reference

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

Happy Learning !!

About Lokesh Gupta

Founded HowToDoInJava.com in late 2012. I love computers, programming and solving problems everyday. A family guy with fun loving nature. You can find me on Facebook, Twitter and Google Plus.

Feedback, Discussion and Comments

  1. Sumit Chouhan

    September 10, 2017

    nice !
    it is very easy to understand.
    Thnaks

    Reply
  2. Ancil Hameed

    July 6, 2017

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

    Reply
  3. Anurag

    July 27, 2016

    clear demonstration… well done..

    Reply
  4. hkv

    June 17, 2016

    Very helpful and easy to understand the complex topic. Thanks

    Reply
  5. Arvind Verma

    February 10, 2016

    great !!!!

    Reply
  6. Hemant

    March 3, 2014

    great work

    Reply
  7. Umamahesh REDDY

    September 25, 2013

    Nice explanation..thanks

    Reply

Ask Questions & Share Feedback Cancel reply

Your email address will not be published. Required fields are marked *

*Want to Post Code Snippets or XML content? Please use [java] ... [/java] tags otherwise code may not appear partially or even fully. e.g.
[java] 
public static void main (String[] args) {
...
}
[/java]

Search Tutorials

  • Email
  • Facebook
  • RSS
  • Twitter

Java Tutorial

  • Java – Introduction
  • Java – JDK, JRE and JVM
  • Java – Naming Conventions
  • Java – ClassPath
  • Java – Variables
  • Java – Operators
  • Java – keywords
  • Java – Data Types
  • Java – Primitive Types
  • Java – Wrapper Classes
  • Java – Types of Statements
  • Java – Control Statements
  • Java – String
  • Java – Create Class
  • Java – main() Method
  • Java – Comments
  • Java – hashCode() and equals()
  • Java – System Properties
  • Java – Pass-by-Value
  • Java – 32-bit vs. 64-bit
  • Java – java.exe vs javaw.exe
  • Java – Generate Bytecode
  • Java – Little-Endian vs Big-Endian
  • Java – Command Line Args
  • Java – Compare floats
  • Java – Static Import
  • Java – Recursion
  • Wrapper Class Internal Caching

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Java Collections
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

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