Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Friday, November 10, 2017

Java Supports Pass By Value only, not Pass By Ref

Does Java supports Pass By Value or Pass By Reference:

 

Very common question in java interviews for freshers and experienced. But, this is very tricky and confusion for most of the people whether java is pass by value or pass by reference.

First, we will make clear what is call by value and call by reference. These two comes into play when we pass parameters in invoking a method.

java-pass-by-value




Call By Value: Values only passed as copy of original. But not original memory location. It creates a new copy in the memory and passes to the method that we invoked.

Call By Reference: Passes reference or alias of the object which is said to be Call By Reference. Unfortunately, Java inventors decided to call the location of an object as a "reference". When we pass the value of an object, we are passing the reference to it. This is confusing to beginners.

Remember, all objects that are created using new operator will be created in heap memory where as all local variables or primitives types are created as part of Stack memory. Stack memory is allocated when a thread starts its execution.

We will demonstrate using the following program.


Pass By Value Example:



package blog.java.w3schools.passbyvalue;

//java-w3schools blog
public class PassByValueExample {

    public static void main(String[] args) {

        int side = 10;
        Square s = new Square();
        System.out.println("side value: " + side);
        s.printArea(side);
        System.out.println("side value After calling printArea is : " + side);
    }
}

class Square {

    public void printArea(int side) {
        side = side * side;
        System.out.println("Inside printArea : " + side);
    }
}


Output:


side value: 10
Inside printArea : 100
side value After calling printArea is : 10

Created a class named Square and method printArea which takes argument side int primitive type.
Once we run the program, main method will start execution which is main thread. side is a int variable and its value is 10.
Created a Square object, printed side value & Could see it as 10.
Now calling printArea method of s and passing side variable. Inside method doing arithmetic operation on it, printing value. it is now 100.
After executing printArea method, controller comes back to the main method and prints side value but we see it as 10 only. That means original variable value is not at all changed. Only copy of variable value was changed.



Pass By Reference Example:


This below example looks like we are passing same reference to the method. You have to remember that a new reference will be created and its passed to it. But, both references will be pointing to the same object in the heap untill it is reassigned with the new object.


package blog.java.w3schools.passbyvalue;

//java-w3schools blog
public class PassByValueExample {

    public static void main(String[] args) {

        Square s1 = new Square(20);
        System.out.println("s1 side value: " + s1.getSide());
        passReference(s1);
        System.out.println("side value After calling printArea is : " + s1.getSide());
    }


    public static void passReference(Square square) {
        square = new Square(30);
        System.out.println("new object value : " + square.getSide());
    }
}


class Square {
    int side;

    public Square(int side) {
        this.side = side;
    }

    public void setSide(int side) {
        this.side = side;
    }

    public int getSide() {
        return side;
    }
}


Output:


s1 side value: 20
new object value : 30
side value After calling printArea is : 20
Please Observe, even though passing the reference to the method, original value is not changed after reassigning with new value inside method passReference.

Summary:


Java supports only Pass By Value but when we passes references and it is not reinitialized then it looks like values are modified of the original object. This is correct. But here you have to catch the point is a new reference is created and pass to the method but original and new references will be pointing to the same object in the head memory location.

No comments:

Post a Comment

Please do not add any spam links in the comments section.