0

I am creating ArrayList l1 and passing l1 reference to the method foo. As we know that Java doesn't support Pass-By-Reference but here its giving different results.

See the below code

public static void main(String[] args) {
    List l1 = new ArrayList(Arrays.asList(4,6,7,8));
    System.out.println("Printing list before method calling:"+ l1);
    foo(l1);
    System.out.println("Printing list after method calling:"+l1);
}

public static void foo(List l2) {
    l2.add("done"); // adding elements to l2 not l1
    l2.add("blah");
}

Output:

Printing list before method calling:[4, 6, 7, 8]
Printing list after method calling:[4, 6, 7, 8, done, blah]
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • 2
    What´s your question? – SomeJavaGuy May 08 '15 at 07:42
  • I am adding elements to l2 in foo method after that i am printing l1 in Main method.Whatever i add to l2 all are able to retrieving in l1. i want to know how it works in compiler. this is pass-by-reference. – Anil Reddy Yarragonda May 08 '15 at 07:45
  • 1
    In practical terms, you can think of primitives as pass-by-value and objects as pass-by-reference. – Steve Chaloner May 08 '15 at 07:45
  • http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – Crembo May 08 '15 at 07:45
  • 4
    @SteveChaloner: No, I'd say that's a *really bad idea*. It encourages all kinds of incorrect mental modelling - and would certainly confuse the OP if they later used a language which has *real* pass-by-reference. All it takes is understanding that the value of `l1` is a reference, not an object, and you don't need to bend what "pass by reference" means at all. – Jon Skeet May 08 '15 at 07:47
  • http://javadude.com/articles/passbyvalue.htm – Ronald Randon May 08 '15 at 07:48
  • @Anil, is it something you want me to clarify in my answer? – aioobe Jun 19 '15 at 13:15

1 Answers1

1

As we know that Java doesn't support Pass-By-Reference but here its giving different results.

Apparently you've heard that Java only supports pass-by-value. This is indeed correct. But what you also have to realize is that Java has references, and that these can be passed by value.

In this case a reference to the list is passed to the method (although the reference is passed by value).

The method then uses this reference to modify the original list, which is why you see the changes on "the outside" as well.

See Is Java "pass-by-reference" or "pass-by-value"?

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • In addition, this only applies to non primitive types and non immutable Objects like the `String` class. – SomeJavaGuy May 08 '15 at 07:55
  • No. Same rules apply to all types of variables. – aioobe May 08 '15 at 08:02
  • But, for example, you wont be able to change the value of an int inside a method given as parameter like he is changing the List in his example, or am i wrong? – SomeJavaGuy May 08 '15 at 08:09
  • ...just as you won't be able to change the references contained in `l1` either. You have to keep in mind that `l1` contains a reference, and not a list. – aioobe May 08 '15 at 08:12