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

后端 未结 6 932
逝去的感伤
逝去的感伤 2020-12-14 04:08
    String[] array = {\"a\",\"c\",\"b\"};
    ArrayList list = new ArrayList();
    list.add(\"a\");
    list.add(\"b\");
    list.add(\"         


        
6条回答
  •  青春惊慌失措
    2020-12-14 04:36

    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.

提交回复
热议问题