Skip to main content

JAVA-Pass By Value Or Reference? Confusion Solved !

This is one of the most confusing topics in Java .The main aim of this post is to solve the confusion in the most simple way possible.
Before going into this discussion, first of all we need to understand the meaning and difference between Pass By Value and  Pass By Reference?
  1. Pass By Value : This concept says that whenever a variable is passed to a method/function , a copy of this variable is passed not the actual variable. So the changes that are made to the variable in method are not reflected in the actual variable.
  2. Pass By Reference : This concept says that whenever a variable is passed to a method/function, actual variable is passed. So the changes that are made to the variable in method are reflected in the actual variable.
Since we have got the basic understanding, lets look at this concept from Java's perspective. In Java there is concept of primitives and non-primitives . Primitives are the basic data types which store the value directly. Non-primitives are defined by programmer and are used as reference to the objects. These references store the memory location of the object to which they refer.

Now let us clear the confusion. JAVA is Pass By Value as simple as that .We will see this HOW ? 

Let us take a example of Student class which has two properties id and name:
Student Model Class
We will create a object of this class in our main class with id 1 and name as "Student1" and assign this to reference std.  Let us assume this object is created at memory location 200. So the reference std is pointing to the memory location of this object .
Main Class
Output is : 

The output of line 12 is :  Std id is : 1 Std name is: Student1   (object at location 200)

When std is passed to method modify at line 16, basically it is the reference value that is being passed which in this case is the memory address of this object 200.So now this object has two references , one is object reference std and other is method reference student.

At line 23, student reference is pointing to object at memory address 200 , which is the original object created at line 8, so the name of this object is updated to Student2 at line 23.

At line 24, a new student object is created with id:2 and name : Student3. Suppose this object is created at memory location 300 , so the reference 's' points to memory location 300.

At line 25, s is assigned to student . It means value of s i.e the memory location is assigned to reference student . So now student reference points to object at memory location 300.

This leads to output at line 27 : Std id is : 2 Std name is: Student3   (object at location 300)

Now at line 18 , the reference std is still pointing to object at address location 200 , so we get the output as  : Std id is : 1 Std name is: Student2    (object at location 200)

This proves that JAVA is Pass By Value .

I hope this gives enough understanding about this topic .If you like this post please do share it !


Comments

  1. Simplified the concept, very nicely put across.

    ReplyDelete
  2. Well explained, Concise & simple way to guide

    ReplyDelete
    Replies
    1. Thanks Rohit. Your comments and feedback is always helpful.

      Delete
    2. If you like this post please do share it

      Delete
  3. Hi bhavya, good explanation for pass by value and pass by reference. Also could you please explain how the hashmap, hashtable and hashset works internally

    ReplyDelete
    Replies
    1. Thanks Bala..Sure I am planning include the hashing and internal hashmap working in my subsequent blogs. Would update you once its ready

      Delete
    2. If you like this post please do share it

      Delete
  4. A very thorough and good explanation Bhavya.

    ReplyDelete
    Replies
    1. Thanks , If you like this post please do share it

      Delete
  5. Good concept. Really good for interview also. To crack a java interview for any fresher on this topic, We can look deeply into this blog. Good Job Done!!

    ReplyDelete
    Replies
    1. Thanks for your feedback.Please do share it.

      Delete

Post a Comment

Popular posts from this blog

Java Constructors

Constructor as the name suggests is used to construct something and here in case of JAVA it is used to construct the object of class. This is the first point where the object is assigned memory in heap and its member variables gets initialized by default values.Whenever we create an object by using new() keyword,it internally gives call to the default constructor of class. Even if you don't declare the default constructor , java compiler provides automatically a default constructor. Let us test this out with a example . We have created a java class Student.java with a test class TestConstructor.java as shown below  : Student.java TestConstructor.java Here we have created a object of Student class at line 9 using new keyword. This gives call to the default constructor of Student class. Note that we have not defined the default constructor explicitly in Student.java . This is provided by java compiler internally. The equivalent code that java compiler provides is as bel

Deeper Insight Into Java Serialization

Sometimes when a object is serialized at one server and deserialized at other server i.e on different JVMs , deserialization may fall and you can come across  InvalidClassException . This seems to be quite weird. Right? This post is aimed at explaining the root cause of this issue and what should be the better approach while implementing  serialization . You can refer to my other blog on Serialization where we have learnt the serialization mechanism and how to implement serialization  in Java to get started on  serialization. Basically , when we serialize any object , the class metadata is also saved along with object's state which is used while that object is deserialized. One thing that is part of this metadata is  serialVersionUID . In my previous post , I told you that it is used for versioning. We will now see what does it actually mean . You must have seen that whenever you implement serializable interface , compiler gives you warning to generate  serialVersionUID( suid )

Serialization In Java - A Complete Guide

This post is aimed to explain the concept of serialization in depth taking into account all the ins and outs that are required to be known to a Java Developer. Basic Usage And Information : Serialization is a very powerful JAVA instrument aimed at saving only the state of a object not its behaviour. This can be used to exchange objects between different systems and networks as is the case in typical enterprise applications.In a way,we can say that we want the object to exist beyond the lifetime of a JVM. Serialization Mechanism : This involves basically below steps : Firstly the metadata related to serializable instance is written which includes name of class,length of class, number of fields,serialversionuid etc. This metadata is written recursively for superclass if any till it reaches to java.lang.Object superclass. This is helpful when deserializing the class. Once metadata is written, the actual data is written. This time it begins from superclass to main class.