6

I was trying the following code,

public void test() {
    List<Integer> list = new ArrayList<>();
    list.add(100);      
    list.add(89);       
    System.out.println(list);
    update1(list);
    System.out.println(list);
    update2(list);
    System.out.println(list);       
}

public void update1(List<Integer> list) {
    list.remove(0);
}

public void update2(List<Integer> list) {
    list = null;
}

I am getting the following output,

[100,89]
[89]
[89]

My question is, why I am not able to assign the list as null inside a called function?

Kajal
  • 709
  • 8
  • 27
  • Thanks @ColonelThirtyTwo, But my question is regrading the null assignment. I know "Java is always pass by value, with no exceptions, ever." from the post "http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value" – Kajal Apr 07 '16 at 12:56
  • What _about_ the null assignment? You're just changing what `list` points to (in the case of `null`, it points to nothing). – Colonel Thirty Two Apr 07 '16 at 22:14

2 Answers2

5

Because all method calls in java are pass by value and that means that the reference got copied when you call another function, but the two are pointing to the same object.

Two reference copies pointing to the same object.

Another Example would be

public void update2(List<Integer> list) {
    list = new ArrayList<>(); // The new refrence got assigned to a new object
    list.add(23); // Add 23 to the new list
}

This above snippet don't affect the old object or it's reference at all.

Ahmed Hegazy
  • 12,395
  • 5
  • 41
  • 64
  • Thanks you @Ahmed. If we use assign with null "new reference getting assigned" right? – Kajal Apr 07 '16 at 12:59
  • no. when you call update2(list) a "new reference get copied" and when you call list = null inside the function you are changing the copied refrence to point to nothing. – Ahmed Hegazy Apr 07 '16 at 13:01
  • Okay, update1(list) is also a "copied reference" right? so how the changes are reflected to the actual reference. – Kajal Apr 07 '16 at 13:08
  • It changes the inner state of the object they both refer to because the two are refering to the same object. – Ahmed Hegazy Apr 07 '16 at 13:10
  • When we assign an object with null(any where), Simply the object reference point to nothing and the previous reference exist. When the garbage collection happens previous reference will be cleared. Am I on the right track? – Kajal Apr 07 '16 at 13:15
  • The object will be garbage collected if and only if there is no references to it. So when you call `list=null` in `update2` function the object won't be garbage collected because there is another reference to it. I don't get if that answers your question, if not please rephrase it. – Ahmed Hegazy Apr 07 '16 at 13:31
1

References, like plain old data types, are passed by value to functions in Java

The update2 just makes the local parameter list refer to null. It does not change what list refers to in the caller to that function.

So update2 is a no-op.

update1 modifies the list through the reference passed to that function.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483