If I return a List in Java, is the return value a reference or the actual value?
You will have a reference to the original ArrayList
.
You can create a shallow copy of the list with clone()
.
Have a look at this question if you want a deep copy.
Everything in java will be a reference by default. So yes changing the returned arraylist will modify the original one.
To prevent that problem you have to create a copy of the original one. Use the .clone() method for that.
If you want a modified list, but not to modify the original, you shouldn't operate on the list which you received in arguments of the method because you operate on reference. Better use something like this:
public void modifyList(List myList) {
myList.add("aaa"); // original *will* be modified
List modifiable = new ArrayList(myList);
modifiable.add("bbb"); // original will *not* be modified - only the copy
}