12

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

Joe
  • 4,460
  • 19
  • 60
  • 106
  • 1
    down voters need explanation, it is valid question – Joe Nov 19 '13 at 05:52
  • It's been asked and answered about 100 times on SO alone (much less elsewhere on the 'net), just look at the "related" list to the right. That's probably why people have downvoted it, as "research effort" would find the answer right here on this site. – T.J. Crowder Nov 19 '13 at 05:59
  • 3
    Java is always `passed by value`. As objects can't be passed, **object references** are `passed by value` – Prateek Nov 19 '13 at 06:18

4 Answers4

18

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.

Rahul
  • 44,383
  • 11
  • 84
  • 103
5

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.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
3

When useDifferentMap(inputMap) is invoked, inputMap is assigned to the parameter Map<String, String> valueMap:

Map<String, String> valueMap = inputMap;

After the assignment, the two references inputMap and valueMap now refer to the same object in the memory, and hence modifying that object via one reference, will be reflected to the other reference.

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
1

Java is pass-byvalue only. No where it is pass-by-reference. inputMap and valueMap(copy of inputMap) are both references to the same hashmap. So, we can access all the methods on the hash map using either of the references - its like two remotes to the same TV.

public static Map<String, String> useDifferentMap(Map<String, String> valueMap) {
            valueMap=null;
        }

try this. If it was pass-by-ref you would have got NPE in the last line of the main method after the call to useDifferentMap()

jai
  • 21,519
  • 31
  • 89
  • 120