How pass-by-reference Is Different From pass-by-value In Java?

In This Tutorial, we will discuss How pass-by-reference is Different From pass-by-value In Java? As both of them are ways to pass the argument in a method in java where the pass-by-value references the exact value of the variable whereas in the case of pass-by-reference means the variable passed in the argument has referred to any other variable in an argument in a method.

pass-by-value

What Is Pass-by-Reference?

As we know that we can pass an argument in a function for making it input for that particular function but there are certain situations where we do not have value to any variable direct but we can refer to some other variable for the input.

And passing such variables as arguments in a function where we do not have the exact value of but we pass the reference of any variable that is what we call pass-by-reference in java.

Let’s understand the same better with the help of an example here.

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);
         
         
    }
}

As we can see in the example above how an object is being passed as a reference.

What is Pass-by-Value?

It is also a way to call a function and pass some argument as reference but it is different in some way that we will discuss here, That is where we directly pass the values or variables that have some value in the argument.

Unlike Pass-by-reference it accesses the value directly with no need to refer to some other address. Let’s understand what exactly pass-by-value is and draw a conclusion to differentiate between both methods of calling the function and resolve the difference between them.

 
    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);
         
         
    }
}

Here we can see how we directly pass a value as an argument that is 50510 as input.

To Learn more about How “pass-by-reference” Is Different From “pass-by-value” In Java? follow: Call by reference and call by value

Also visit Python’s list: What is the difference Between methods Append and Extend? and

What is the purpose of the return statement? How is it different from printing? 

And learn more about python and java and solve you problems here.

Leave a Comment

%d bloggers like this: