The toString() method of collection:

Consider this example:
import java.util.*;
class demo
{
public static void main(String[] args){
ArrayList al=new ArrayList();
al.add("bhabani");
al.add("pinku");
al.add("mahesh");
al.add("arvind");
al.add("arjan");
System.out.println(al);
}};

We can print the elements of the object by simply
System.out.println(al);
By directly printing the object.
This is because the toString() method for collection is defined in AbstractCollection and it is inherited by the ArrayList.
So the toString() in java.lang.Object is different from java.util.Collection.

No comments:

Post a Comment