9

Object.toString() JavaDoc says:

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object.

Still, so many of the standard java classes like Collections (Sets, Lists etc) that could have a very useful toString() methods, don't bother implementing it. Is there a reason for this stupidity?

Or hey, would you like a hex string instead? :)

EDIT: Oops, this was my failure in using my IDE. I followed the interface instead of the implementation and that took me for some reason straight to Object.toString().

vertti
  • 7,539
  • 4
  • 51
  • 81

2 Answers2

6

UPD:

If you mean Collection classes, so answer will be: this is not exactly like this. Many of the Collection classes override this method; for example, the AbstractCollection class, which has its own toString implementation - common for all inherited classes.

If you mean Collection[s], then this class has a private constructor and can not be instantiated; so a special toString method is pointless.

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
  • 3
    he mentioned Collections which a util class not Collection which is an interface. – gregory561 Dec 19 '12 at 08:31
  • 1
    @gregory561, I'm afraid, that author made a misprint. – Andremoniy Dec 19 '12 at 08:34
  • in that case a question does not make any sense as the Collection interface does not extends/implement Object? which has toString method. Even if it did it is impossible to implement this method in an iterface – gregory561 Dec 19 '12 at 08:37
2

If You are talking about Collections class, it is utility class, it is not necessary to have toString() method overridden. Generally in case of utility classes we make constructors private and provide static methods.

Also check you can not create an object of Collections class becuase it's constructor is made private. Check java.util.Collections source, Line number 56

Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85