@Arunesh_Singh
Your example is different that his. Replace Integer with List in @Ye_Shao’s swap example, it will behave the same.
Integer class doesn’t have methods like add, remove, replace, set that List has. For example you can’t do this.
Integer i = new Integer(10);
i.set(20); //there is no set method because you are not allowed to change the value within the object once it is created.
That’s why it’s called immutable. Again you are mixing it up, pass by reference behaviour has nothing to do with immutability.
One can create a custom list class that is immutable. Which means once list is created with items, you can’t add or remove items from it. Google’s Guava library has such list.
https://guava.dev/releases/23.0/api/docs/com/google/common/collect/ImmutableList.html
doc of add(int index, E element)
method for immutable list says
Deprecated.
Unsupported operation.