String[] array = {"a","c","b"};
ArrayList<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
System.out.println(array);
System.out.println(list);
For list [a, b, c] is output while for array some address is output. When we want to output the array values, we can use Arrays.toString(array); which works just like list.
I just wonder why we can’t call toString() directly on array to get the values. Isn’t it more intuitive and convenient to do so? What results in the different treatments on Array and ArrayList?
The main difference between an array and an arraylist is that an arraylist is a class that is written in Java and has its own implementation (including the decision to override
toString) whereas arrays are part of the language specification itself. In particular, the JLS 10.7 states:In other words the language specification prevents the
toStringmethod of an array to be overriden and it therefore uses the default implementation defined inObjectwhich prints the class name and hashcode.Why this decision has been made is a question that should probably be asked to the designers of the language…