I am new to java and I'm trying to take some exams to test my knowledge. One particular question kind of gotten under my skin (lol).
List<String> x = new ArrayList<String>();
x.add("A");
x.add("B");
List<String> y = x;
y.add("C");
System.out.println(x);
System.out.println(y);
The output I would have expected is
[A, B]
[A, B, C]
But apparently, whatever you do with Y will also happen to X. Both of them have this value now:
[A, B, C]
My first question is why? Would you happen to know any reference that I can study as to when to expect this to happen in a Java object?