Consider following case.
List<Integer> listOne = new ArrayList<>();
List<Integer> listTwo = new ArrayList<>();
listOne.add(1);I think this happens due to
listOne.add(2);
listOne.add(3);
Collections.reverse(listOne);
listTwo = listOne; //listTwo has same reference
Collections.reverse(listOne);
System.out.println(listOne); //out put [1, 2, 3]
System.out.println(listTwo); // same out put
Java is pass by value, where values (for non primitive types) happen to be references.
I think this provide survival for java for this kind of scenario. To be honest why java
try to avoid pass by reference
and try to be different from some of other languages
? while java still suffering from pass by reference behaviors?
Edit: additionally please some one explain what happen in above code