String[] array = {\"a\",\"c\",\"b\"};
ArrayList list = new ArrayList();
list.add(\"a\");
list.add(\"b\");
list.add(\"
The short answer is because toString is defined in a few different places, with different behaviours.
The Arrays class defines toString as a static method, to be invoked like
Arrays.toString(arr_name);
But the Arrays class also inherits the non-static method toString from the Object class. So if called on an instance, it invokes Object.toString which returns a string representation of the object (eg: [Ljava.lang.Object;@4e44ac6a)
So Arrays.toString() and MyObject.toString() are calling different methods with the same name.
The ArrayList class inherits toString from the AbstractCollection class, where it is a non static method, so can be called on the object like:
MyArrayList.toString();
Because it's a string representation of a collection and not an object, the result is the values in a readable format like [one, two].
This is the toString method call for ArrayList. But for Arrays you cant find such.
/**
* Returns a string representation of this collection. The string
* representation consists of a list of the collection's elements in the
* order they are returned by its iterator, enclosed in square brackets
* (<tt>"[]"</tt>). Adjacent elements are separated by the characters
* <tt>", "</tt> (comma and space). Elements are converted to strings as
* by {@link String#valueOf(Object)}.
*
* @return a string representation of this collection
*/
public String toString() {
Iterator<E> it = iterator();
if (! it.hasNext())
return "[]";
StringBuilder sb = new StringBuilder();
sb.append('[');
for (;;) {
E e = it.next();
sb.append(e == this ? "(this Collection)" : e);
if (! it.hasNext())
return sb.append(']').toString();
sb.append(',').append(' ');
}
}
I just wonder why we can't call toString() directly on array to get the values.
Actually toString
method is called on the array object. But, since array type does not override toString
method from Object
class, so default implementation of toString
is invoked, that returns the representation of the form that you see.
The representation is of the form: -
[typeOfArray@hashCode
In your case it's something like: -
[Ljava.lang.String;@3e25a5
Whereas, in case of ArrayList
instances, the overriden toString
method in ArrayList
class is invoked.
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:
The members of an array type are all of the following:
- The public final field length
- The public method clone, which overrides the method of the same name in class Object and throws no checked exceptions.
- All the members inherited from class Object; the only method of Object that is not inherited is its clone method.
In other words the language specification prevents the toString
method of an array to be overriden and it therefore uses the default implementation defined in Object
which 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...
Because when you print toString()
, it will by default print className@HashCode
.
So, when you print array
then above will be printed.
But ArrayList
is extened by AbstractCollection
class and where the toString()
method is overriden as below
public String toString() {
Iterator<E> it = iterator();
if (! it.hasNext())
return "[]";
StringBuilder sb = new StringBuilder();
sb.append('[');
for (;;) {
E e = it.next();
sb.append(e == this ? "(this Collection)" : e);
if (! it.hasNext())
return sb.append(']').toString();
sb.append(',').append(' ');
}
}
which prints the readable format of the ArrayList
object.
For array
toString() method it prints the memory address. But in ArrayList
this class overrides Object
toString() method .
toString() implementation of ArrayList
public String toString() {
Iterator<E> i = iterator();
if (! i.hasNext())
return "[]";
StringBuilder sb = new StringBuilder();
sb.append('[');
for (;;) {
E e = i.next();
sb.append(e == this ? "(this Collection)" : e);
if (! i.hasNext())
return sb.append(']').toString();
sb.append(", ");
}
}