URI / URL and String, what is the difference?

This is out of curiosity, no code is required, I tried to use the best search engine in the world to get answers, but did not understand anything useful.

What is the difference between a URI/URL and a string representing this URI / URL?

Why should we take this apart?

What does parsing a string in the URI/URL for a string make it handle differently?

Why does HTTP get string input text support?

+6
source share
1 answer

URI / URL is limited. To be valid, it must follow a specific format.

Saying that they accept only URI / URL methods, they say that the string they accept should be in this format. It also makes it easier to understand at a glance what the method expects (URL), and not potentially any String.

If the string is not in this format, it is detected immediately when you try to create a URL object, and not at some unknown point in the future when you try to use this URL in the method.

Thus, by doing this, it improves type safety, makes the code more readable, and leads to crashes that may occur closer to the root cause of the failure, and not to some unknown point in the future.

You also get methods for correctly creating a URL from its parts, for opening resources using a URL, etc. So this is much more than just a string. Take a look at Javadoc to find out how many methods it provides:

http://docs.oracle.com/javase/7/docs/api/java/net/URL.html

+5
source

All Articles