In this example
public static void main(String[] args) { List<Integer> integers = new ArrayList<Integer>(); integers.add(1); addToList(integers); System.out.println(integers); } public static void addToList(List list0){ list0.add("blabl"); }
Compiles and prints the result
[1, blabl]
My understanding:
The control variable 'integers' has the address (say 111) of the arraylist object, which is passed to the addToList method. Thus, in the addToList method, list0 points to the same address that has an object (which is an arraylist of type Integer), and a string is added to this arraylist object.
How can I add a String to an integer type arraylist? Isn't that a data integrity issue?
Update
The answer below and this answer helped. Thanks.
source share