Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

    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?

like image 217
Terry Li Avatar asked Dec 08 '12 18:12

Terry Li


People also ask

Does ArrayList have toString method?

Note: The ArrayList class does not have its own toString() method. Rather it overrides the method from the Object class.

Does toString work on arrays?

JavaScript calls the toString method automatically when an array is to be represented as a text value or when an array is referred to in a string concatenation.

What is the purpose of the toString () method in object Why is it useful?

toString() method returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read.

Why toString method is override in Java?

Overriding toString() Method in Java out. print. It is because this method was getting automatically called when the print statement is written. So this method is overridden in order to return the values of the object which is showcased below via examples.


2 Answers

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

like image 53
assylias Avatar answered Sep 20 '22 04:09

assylias


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.

like image 32
Rohit Jain Avatar answered Sep 19 '22 04:09

Rohit Jain