Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Pintu Bara
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pavan Kumar Dittakavi
Ranch Hand
Posts: 106
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 106
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the clarification Jeff. Yes all classes have toString method with them either from Object or overridden.
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Small addition: you can get the same output for an array by using one of the java.util.Arrays.toString() methods.
 
Chandraprakash Sarathe
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic