Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a semantic difference between toExternalForm and toString on java.net.URL?

Tags:

java

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.

like image 425
Hugh Avatar asked Apr 21 '20 14:04

Hugh


People also ask

What is toExternalForm?

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.

Which one constructs a string representation of URL?

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.


2 Answers

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:

  1. The Object.toString() method has a very loose specification. It basically just returns something that may be useful for debugging.

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

  3. Having designed and implemented URL.toExternalForm() someone probably thought:

    "Oh ... now I have a good way to implement URL.toString()".

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

like image 58
Stephen C Avatar answered Oct 18 '22 06:10

Stephen C


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.

like image 42
Robert Avatar answered Oct 18 '22 05:10

Robert