Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Pass By Value and Pass By Reference [duplicate]

i have done the following sample with to check my knowledge

import java.util.Map;

public class HashMap {
    public static Map<String, String> useDifferentMap(Map<String, String> valueMap) {
        valueMap.put("lastName", "yyyy");
        return valueMap;
    }

    public static void main(String[] args) {
        Map<String, String> inputMap = new java.util.HashMap<String, String>();
        inputMap.put("firstName", "xxxx");
        inputMap.put("initial", "S");
        System.out.println("inputMap : 1 " + inputMap);
        System.out.println("changeMe : " + useDifferentMap(inputMap));
        System.out.println("inputMap : 2 " + inputMap);
    }
}

the output is :

original Map : 1 {initial=S, firstName=xxxx}
useDifferentMap : {lastName=yyyy, initial=S, firstName=xxxx}
original Map : 2 {lastName=yyyy, initial=S, firstName=xxxx}

this method useDifferentMap gets the map and changes the value and returns back the same. the modified map will contain the modified value and the scope of it is local for the useDifferentMap method.

my question is if java is pass by value is the modified value should not be affected in the original map.

so is java pass by value or pass by reference ???

thanks

like image 721
Joe Avatar asked Nov 19 '13 05:11

Joe


People also ask

What is difference between pass by value and pass by reference in Java?

Pass by Value: The method parameter values are copied to another variable and then the copied object is passed, that's why it's called pass by value. Pass by Reference: An alias or reference to the actual parameter is passed to the method, that's why it's called pass by reference.

What is pass by value and pass by reference in Java with example?

Pass by Value: It is a process in which the function parameter values are copied to another variable and instead this object copied is passed. This is known as call by Value. Pass by Reference: It is a process in which the actual copy of reference is passed to the function. This is called by Reference.

What is difference between pass by value and pass by reference?

"Passing by value" means that you pass the actual value of the variable into the function. So, in your example, it would pass the value 9. "Passing by reference" means that you pass the variable itself into the function (not just the value). So, in your example, it would pass an integer object with the value of 9.

What is pass by value and pass by reference does Java support both of them?

Pass-by-value means that the value of a variable is passed to a function/method. Pass-by-reference means that a reference to that variable is passed to the function. The latter gives the function a way to change the contents of the variable. By those definitions, Java is always pass-by-value.


2 Answers

Java always uses the pass-by-value concept to pass the arguments. In the scenario mentioned, the reference to the HashMap itself is passed by value. The valueMap refers to the same object as the inputMap since both of them are referring to the same object.

That's why when you add a key-value pair using valueMap, it is reflected back in inputMap.

Checkout out this simple, yet nicely written answer by Eng.Fouad for a picturized version of the concept. Feel free to read a few more answers in that same question that has more in-depth information.

like image 72
Rahul Avatar answered Sep 27 '22 17:09

Rahul


Java is pass-by-value. But your doubt is reffering to reference, Even reference in java passed by value.

So reference value passed, and the map gets effected.

You confused with the term pass by value. pass by value in the sense reference passed as value.

like image 43
Suresh Atta Avatar answered Sep 27 '22 18:09

Suresh Atta