wood burning stoves 2.0*
The moose likes Java in General and the fly likes Why I get the List content when I print the List object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why I get the List content when I print the List object?" Watch "Why I get the List content when I print the List object?" New topic
Author

Why I get the List content when I print the List object?

Pintu Bara
Greenhorn

Joined: Mar 01, 2010
Posts: 4


Hi,

Can any one explain me the following output?


The ArrayList class internally maintain an array. When I print ArrayList object I get the list element seperated by comma where as in case of array object I get the memore address. why the ArrayList element is printed seperated by comma and I don't get the memory address just like array when I print the list object?

Thanks,
Pintu Bara
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1505
    
    5

Hi Pintu,

Welcome to coderanch!

This behaviour is because of the ways in which toString() method is implemented in class Object and class ArrayList (ArrayList overrides Object's toString() method).

toString() of Object prints class name, followed by '@' symbol, followed by hashCode of that object.

toString() of ArrayList prints all the members of the list within square brackets, and members are separated by comma (','). How each member of that list is printed will further be decided on that class' toString() method. e.g. while printing ArrayList of Employee, first toString() of ArrayList will be called, followed by toString() method for all members (i.e. toString() method of Employee class - if it is overridden. Otherwise, toString() of Object will be called).

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Pavan Kumar Dittakavi
Ranch Hand

Joined: Feb 12, 2011
Posts: 104

Hi Pintu,

The println method checks if the object that it needs to print has a toString method defined. If it has then it uses that. The fact that you are able to see the list contents when using the println method should state the same. So, please check if the List class has the method toString overridden.

However, if an object doesn't have this method overriden, the toString() method from Object class gets picked up and we get to see the hashcode. Verify Objects toString method.

Thanks,
Pavan.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Pavan Kumar Dittakavi wrote:Hi Pintu,

The println method checks if the object that it needs to print has a toString method defined.


No, it doesn't. All objects have a toString() method, and println() just calls it; it doesn't check first. (Actually, it's not toString() that calls it, but some other method that toString() calls, like String.valueOf().)
Pavan Kumar Dittakavi
Ranch Hand

Joined: Feb 12, 2011
Posts: 104

Thanks for the clarification Jeff. Yes all classes have toString method with them either from Object or overridden.
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
Small addition: you can get the same output for an array by using one of the java.util.Arrays.toString() methods.
Chandraprakash Sarathe
Greenhorn

Joined: Jan 21, 2012
Posts: 16
ArrayList overrides Object's toString() method


I was surprised to see ArrayList doesn't override toString() method instead it is overridden by java.util.AbstractCollection class which is parent of ArrayList.

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/AbstractCollection.html#toString()


Chandraprakash Sarathe
http://javaved.blogspot.com
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Chandraprakash Sarathe wrote:
ArrayList overrides Object's toString() method


I was surprised to see ArrayList doesn't override toString() method instead it is overridden by java.util.AbstractCollection class which is parent of ArrayList.


Why is that surprising?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why I get the List content when I print the List object?