As far as I am aware, in Java reference types are assigned by value, but I can't understand why the following code does what it does. Consider the following code:
Integer num = new Integer(11);
List<Integer> arr = new ArrayList<>();
arr.add(num);
List arr2 = arr;
arr = null;
System.out.println(arr2);
We create an ArrayList with one element in it, then we create the second list and assign it to the first list. My understanding that the second list just points to the same memory location as the first list, so any changes I make to the first list will apply to the second list. If I set the first list to null the second list should become null as well. Why does the console still log 11 and not null?