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]