Does String's toString() method have any practical purpose?

Besides filling conventions or using computing resources, what does a .toString() in Java do on a String that using the String itself wouldn't?

It means it gives an appropriate result when called polymorphically.

Why doesn't it simply inherit the .toString() from class java.lang.Object?

Because that wouldn't give the same result, and almost certainly not the desired result.

Object.toString() is meant to give a reasonably useful string representation of the object. The Object implementation gives information about the type and a value which can be used for crude identity hints (diagnostically useful, but that's all). That's clearly not the most useful string representation for a string - the string itself is.

While I would say that it's a pity that toString is defined in quite a woolly way (it's not clear whether the result is meant for machine, developer or user consumption), it feels obvious to me that a String would return itself in the implementation.


Practical example:

public abstract class MetadataProcessor {

protected void processMetadata() {    
   Map<String, Object> metadata = getMetadata();
   for(String key : metadata.keySet()) {
      if(metadata.get(key) instanceof Date) {
      processDate(metadata.get(key));
   } else { //String or any other object type
      processString(metadata.get(key).toString());
   }
}

private void processDate(Date date) {
   (...)
}

private void processString(String string) {
   (...)
}

/**
* contains document's metadata, values must be String or Date
**/
protected abstract Map<String, Object> getMetadata();
}

If String's .toString() wouldn't return the String itself, an additional if(foo instanceof String) would be needed in above code.