0

I think I passed the reference of value to hashmap,then after I change the value outside,the value in the hashmap should also be changed.

    Integer key=new Integer(0);
    Integer value=new Integer(3);
    HashMap<Integer,Integer> hm=new HashMap<Integer,Integer>();
    hm.put(key, value);
    System.out.println(hm.get(key));
    value=null;
    System.out.println(hm.get(key));
Tony
  • 11
  • 3
  • You are not changing the value of `Integer value` you are changing its reference, and `Integer` is immutable, so you will need another `put` to replace – Scary Wombat Jul 14 '17 at 02:38
  • Java is pass by value and not by reference. See https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – Michael Markidis Jul 14 '17 at 02:40

0 Answers0