The implementation of one simply delegates to the other, which suggests to me that there is a semantic difference between the two from an interface standpoint -- or at least, someone thought so at some point. Can anyone shed some light there?
Edit: I already know the implementation of toString
delegates to toExternalForm
. It's the first thing I said. :) I'm asking why this duplication exists - that's what I meant by "semantic" difference.
The toExternalForm() function is a part of URL class. The function toExternalForm() returns the string representation of a specified URL. The string is created by calling the toExternalForm() function of the stream protocol handler for this object.
toExternalForm. Constructs a string representation of this URL . The string is created by calling the toExternalForm method of the stream protocol handler for this object.
The javadocs state this for both toString()
and toExternalForm()
,
Constructs a string representation of this URL. The string is created by calling the
toExternalForm
method of the stream protocol handler for this object.
In other words, the two methods are specified to return the same value.
Why?
It would be difficult to find the real reason that URL
API was designed this way. The decisions were made ~25 years ago. People won't remember, and meeting notes (if they were taken) have probably been lost or disposed of.
However, I imagine the reasoning would have gone something like this:
The Object.toString()
method has a very loose specification. It basically just returns something that may be useful for debugging.
The designers probably decided that they wanted a method that has a clear and specific behavior for stringifying a URL
object. They called it URL.toExternalForm()
.
Having designed and implemented URL.toExternalForm()
someone probably thought:
"Oh ... now I have a good way to implement
URL.toString()
".
Finally, they probably decided to specify that the two methods return the same thing.
The decision to specify that the two methods return the same thing was made between Java 1.0 and Java 1.1. (Google for the Java 1.0 and 1.1 documentation and look at the respective javadocs.)
This suggests that step 4 was done "after the fact" of the original implementation. (We would need to look at the original source code and commit history to confirm that, and it is not available.)
The OpenJDK code contains the answer:
There is absolutely no difference between java.net.URL.toString()
and java.net.URL.toExternalForm()
as toString()
just calls toExternalForm()
:
public final class URL implements java.io.Serializable {
...
public String toString() {
return toExternalForm();
}
...
public String toExternalForm() {
return handler.toExternalForm(this);
}
Source
The question WHY is a different topic. Both methods have not been changed for more than 13 years. Also some Java 1.1 documentation that is still online indicates that both methods were designed to return the same result right at the beginning of Java. Most likely the toExternalForm()
is the correct method to get a String representation of an URL and for convenience the toString()
method just returns the same result as toString() is way more often used by most Java developers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With