0

why name of array "list" first printed as reference and second code printed content of it.

int[] list = new int[5];
list[0] = 1;
list[1] = 2;
System.out.println(list);

It prints the reference [I@4dd8dc3 and i know that i should use for loop to iterate over elements.

But why there is in other code gives me the content not the address? Is that specific for generics?

ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
System.out.println(list);

that prints [1, 2]

Run
  • 130
  • 6
  • You are mixing *lists* with *arrays*. `ArrayList` redefines `toString()` to print list contents, whereas an array object does not. – Janez Kuhar Feb 02 '21 at 01:34
  • 1
    *"Is that specific for generics?"* - Nope. Generics have nothing to do with it. It is all about how (or where) the `toString()` method is implemented for the object you are printing. – Stephen C Feb 02 '21 at 01:43

0 Answers0