I am having trouble understanding how objects are added to a List.
This is a sample of code where an int[] is added to a List -
List<int[]> list = new ArrayList<>();
int[] element = {2,3};
list.add(element);
System.out.println(list); //[[2,3]]
element[1] = 45;
System.out.println(list); //[[2,45]]
I am not sure how exactly objects are added to a List. Is a copy of the Object added? I was trying to find if it works the same for primitives, so here is a code sample for that -
List<Integer> list = new ArrayList<>();
int obj = 25;
list.add(obj);
System.out.println(list); //[25]
obj=56;
System.out.println(list); //still [25]
The add function documentation on Oracle doesn't mention how the objects are added, just that it is a boolean function that adds the element at the end unless otherwise specified. I know that Java works with reference to objects, but with respect to this problem, what does it mean exactly?
Will be obliged if you could help me understand this, thank you in advance