Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Java standard libraries implement toString methods? [closed]

Tags:

java

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().

like image 917
vertti Avatar asked Dec 19 '12 08:12

vertti


2 Answers

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.

like image 168
Andremoniy Avatar answered Oct 30 '22 08:10

Andremoniy


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

like image 34
Nandkumar Tekale Avatar answered Oct 30 '22 09:10

Nandkumar Tekale