• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

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

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 112
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 112
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the clarification Jeff. Yes all classes have toString method with them either from Object or overridden.
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic