This is probably very trivial to many of you, but I could not find the answer to it. Thanks in advance for any help.
In short, I shallow copy a list, then modify one of its elements, but the change is not reflected in the other list, despite almost every resource saying that they are just two references to the same object, and any change in one should be reflected in the other one.
P.S. I have already checked out questions like this one that clarify the differences b/w shallow and deep copy. My question is why despite those theories, sometimes shallow-copy behaves like deep-copy.
List<Integer> lst1 = new ArrayList<>();
lst1.add(2);
lst1.add(6);
lst1.add(1);
lst1.add(4);
lst1.add(9);
lst1.add(5);
List<Integer> lst2 = new ArrayList<>(lst1);
lst1.set(0, 3);
System.out.println("lst1 = " + lst1);
System.out.println("lst2 = " + lst2);
result:
lst1 = [3, 6, 1, 4, 9, 5]
lst2 = [2, 6, 1, 4, 9, 5]