-1

I have searched a lot and every where found that java is pass by value but still I am not satisfied with the answers. Below is the code. Here if i am passing the object of HashMap then I am getting its updated value while in case of integer it is not like that. What is the difference between these both. How pass by value is working in both cases-

public static void main(String[] args) {
    HashMap hm = new HashMap();
    System.out.println(hm.size()); //Print 0

    operation(hm);
    System.out.println(hm.size()); //Print 1 ; Why it's updating the HashMap.

    Integer c = new Integer(3);
    System.out.println(c); //Print 3

    getVal(c);
    System.out.println(c); //Print 3: Why it's not updating the integer like HashMap.
}

public static void operation(HashMap h) {
    h.put(“key”, “java”);
}

public static void getVal(Integer x) {
    x = x + 2;
    System.out.println(x);
}
Tiny
  • 27,221
  • 105
  • 339
  • 599

3 Answers3

2

Here if i am passing the object of hashmap

You aren't. You're passing a reference to the hashmap. The reference is passed by value.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thanks for your response. But i am doing same in case of Integer also then why it not updating.What is the difference between both these. – user3400568 Mar 10 '14 at 06:44
  • There is no difference. The parameter is passed by value. What you're doing with the HashMap reference is modifying the original object, but not via assignment, so the cases aren't comparable. – user207421 Mar 10 '14 at 06:45
  • Thanks again EJP!! You have almost taken me to the satisfactory answer. One more doubt i want to clear. In case of Integer c, is c is not the reference ? Does it will not modify the original object like HashMap ? – user3400568 Mar 10 '14 at 06:51
  • *If* there were any mutator methods of `Integer,` which there aren't, the answer would be 'yes'. As it is, no. – user207421 Mar 10 '14 at 06:53
1

In java- "References to objects are passed by value".

Flow :

public static void main(String[] args) {
    HashMap hm = new HashMap();
    System.out.println(hm.size()); //Print 0 

    operation(hm);
    System.out.println(hm.size()); //Print 1 ; Why it's updating the HashMap.

    Integer c = new Integer(3);
    System.out.println(c); //Print 3

    getVal(c);
    System.out.println(c); //Print 3: Why it's not updating the integer like HashMap.
}

public static void operation(HashMap h) {  --> You are modifying the object pointed by hm. now both references hm and h  point to the same HashMap
    h.put(“key”, “java”);
}

public static void getVal(Integer x) {  // here X and c both point to same Integer object.
    x = x + 2;   // Now X and c point to different Integer objects. and X will not be visible outside this method. So the value of c will not change.               
    System.out.println(x);
}
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0

As for the reference type parameters, the reference is passed by value which means that you cannot modify the reference itself (e.g. you cannot set it to null).

You can still modify the object that the reference points to (e.g. modify its properties).

Szymon
  • 42,577
  • 16
  • 96
  • 114