10

This is out of curiosity, no code required, I tried using the worlds best search engine to get my answers but figured out nothing worthwhile.

Whats the difference between a URI/URL and a string representing that URI/URL?

Why do we have to parse it?

What does parsing a string into URI/URL do to the string such that it is treated differently?

Why does HTTP get support typing URL as a string?

gowtham
  • 949
  • 7
  • 15
Skynet
  • 7,522
  • 5
  • 40
  • 78
  • You can find your answer here http://stackoverflow.com/questions/176264/whats-the-difference-between-a-uri-and-a-url – Tareq Salah Jan 09 '14 at 10:25
  • You misinterpreted, I am taking on the difference between URI/URL and String representation of the same. Precisely (URI/URL) vs String. – Skynet Jan 09 '14 at 10:26
  • Then you'll have to specify which implementations you are trying to compare etc. For example, there is `java.net.URI` and there's `org.apache.commons.httpclient.URI` – Ceiling Gecko Jan 09 '14 at 10:27
  • Let us keep it generalized, I work on Android use Java URI for me is a local resource such as a contact's image path, URL would refer to when I try to access a webserver. – Skynet Jan 09 '14 at 10:28

1 Answers1

10

URI/URL is constrained. To be valid it has to follow a certain format.

By saying that they only accept a URI/URL methods are saying that the string they accept has to be in that format. This also makes it clearer "at a glance" what the method is expecting (a URL) rather than potentially any String.

If the String isn't in that format then it is detected immediately when you try and create the URL object rather than at some unknown point in the future when you try and use that URL in a method.

So by doing this it increases type safety, makes code more readable, and causes failures to happen closer to the root cause of the failure rather than at some unknown point in the future.

You also get methods to construct the URL properly from its parts, to open resources using the URL, etc. So it's a lot more than just a String. Take a look at the Javadoc to see how many methods it provides:

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

Tim B
  • 38,707
  • 15
  • 73
  • 123
  • Means Parsing is only a means of validation? Should the compiler accept a string in such a format but is not parsed. The basic confusion is what does parsing do in this scenario? – Skynet Jan 09 '14 at 10:32
  • I just added a bit more info on other reasons. I don't really understand your comment and what you are now asking though. – Tim B Jan 09 '14 at 10:44
  • Reading on the link you posted. I was a bit confused as to what does parsing cause to a string to be treated differently and be known as a URI. – Skynet Jan 09 '14 at 10:48
  • It doesn't. It creates a new URI object that is built from the String you pass in. – Tim B Jan 09 '14 at 10:50