Saturday, August 20, 2022

Java Pass By Reference And Pass By Value

Reference

Java Pass By Value Example

In this example, we will showcase how to pass a parameter by using pass-by-value which is also known as call-by-value.

Here we have initialized a variable ‘a’ with some value and used the pass-by-value technique to show how the value of the variable remains unchanged. In the next segment, we will try to show a similar example, but we will use non-primitives.

public class Example {
     
    /*
     *  The original value of a will remain unchanged in
     *  case of call-by-value
     */
     
    int a = 10;
    void call(int a) {
         
        // this local variable a is subject to change in its value
        a = a+10;
    }
     
    public static void main(String[] args) {
 
        Example eg = new Example();
        System.out.println("Before call-by-value: " + eg.a);
         
        /*
         * Passing an integer 50510 to the call() method. The value of
         * 'a' will still be unchanged since the passing parameter is a
         * primitive type.
         */
        eg.call(50510);
        System.out.println("After call-by-value: " + eg.a);
         
         
    }
}

Output:

Java pass by value

Java Passing Object: Pass by Reference Example

In this example, we will see how to pass any object of a class using pass-by-reference.

As you can see, when we have passed the object reference as a value instead of a value, the original value of the variable ‘a’ is changed to 20. This is because of the changes in the called method.

public class Example {
     
    /*
     *  The original value of 'a' will be changed as we are trying
     *  to pass the objects. Objects are passed by reference.
     */
     
    int a = 10;
    void call(Example eg) {
        eg.a = eg.a+10;
    }
     
    public static void main(String[] args) {
 
        Example eg = new Example();
        System.out.println("Before call-by-reference: " + eg.a);
         
        // passing the object as a value using pass-by-reference
        eg.call(eg);
        System.out.println("After call-by-reference: " + eg.a);
         
         
    }
}

Output:

Java passing Object - pass by Reference

Ways To Create A Pass-by-Reference

Java supports pass-by-value,0 but there are three different ways to create a pass-by-reference in Java.

  • Make the member variable public inside a class.
  • Return a value from a method and update the same inside the class.
  • Create a single element array and pass it as a parameter to the method.

Making The Member Variable Public

In this technique, the object of a class is passed to the add() method and it updates the public member variable ‘a’. You can see that the original memory address where the value is stored has been changed.

public class Example {
     
    // making a public member variable
    public int a;
     
    public Example() {
        a = 10;
    }
     
    public static void main(String[] args) {
 
        Example eg = new Example();
         
        // Before calling the add() method
        System.out.println("Before calling method: " +eg.a);
         
        // calling method
        add(eg);
         
        // After calling the add() method
        System.out.println("after calling method: " +eg.a);
         
    }
     
    // add() method starts here that increments 'a' by 1
    public static void add(Example obj) {
        obj.a++;
    }
     
}

Output:

Making the member variable Public

Returning A Value From A Method

In this technique, we are trying to return a value from add() method as we have changed the type from “void” to “int”. The changes or addition in the value is returned by the add() method and the original memory address has been updated.

public class Example {
     
    public static void main(String[] args) {
         
        int a = 10;
         
        // Before calling the add() method
        System.out.println("Before calling method: " +a);
         
        // calling method
        a = add(a);
         
        // After calling the add() method
        System.out.println("after calling method: " +a);
         
    }
     
    // add() method starts here that increments 'a' by 1
    public static int add(int a) {
        a++;
        return a;
    }
     
}

Output:

Returning a value from a method

Creating A Single Element Array & Passing As A Parameter

In this technique, we have created a single element array and passed it as a parameter to the method add(int a[]). You can see that the original memory address is changed in this case as well.

public class Example {
     
    public static void main(String[] args) {
         
        // single element array
        int a[] = {10};
         
        // Before calling the add() method
        System.out.println("Before calling method: " +a[0]);
         
        // calling method
        add(a);
         
        // After calling the add() method
        System.out.println("after calling method: " +a[0]);
         
    }
     
    // add() method starts here that increments 'a' by 1
    public static void add(int a[]) {
        a[0]++;
    }
     
}

Output:

Creating a single element array

Frequently Asked Questions

Q #1) Can you pass by reference in Java?

Answer: Java supports pass by value and we can not pass primitive types to a method directly by using pass by reference. However, there are different ways to create a pass by reference as discussed above.

Q #2) Does Java pass arrays by reference?

Answer: Java supports pass by value but when we are dealing with objects such as Java array objects, then the object reference is passed to the method.

Q #3) Does Java pass objects by reference or value?

Answer: This won’t be wrong to say that the “Objects in Java are passed by reference”. But if you want a technically correct statement then the above statement can also be put as “Object references in Java are passed by value”.

Q #4) Explain why there is no call by reference in Java.

Answer: Call by reference needs the memory location to be passed and these memory locations further require pointers which Java does not have. Hence, there is no call by reference in Java.

Q #5) Why pointers are not used in Java?

Answer: Unlike the C language, Java does not have pointers. The prime reason for not using pointers in Java can be security as pointers may compromise the security that comes along with Java. The use of Pointers might have made Java more complex.

No comments:

Post a Comment

Programming in Java (Semester-5)

  Programming in Java (Semester-5) Syllabus Unit I Unit II Unit III Practical Question Bank Java Practical Programs Java Important Topics Qu...