Why toString() method works differently between Array and ArrayList object in Java

57,420

Solution 1

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...

Solution 2

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.

Solution 3

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].

Share:
57,420
Terry Li
Author by

Terry Li

Updated on March 09, 2020

Comments

  • Terry Li
    Terry Li about 4 years
        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?