Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URI/URL and String what is the difference?

Tags:

java

url

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?

like image 545
Skynet Avatar asked Jan 09 '14 10:01

Skynet


People also ask

What is the difference between URI and URL?

URL and URI, both crucial concepts of the web, are terms that are often interchanged and used. However, they are not the same. The URI can represent both the URL and the URN of a resource, simultaneously, while URL can only specify the address of the resource on the internet.

What is a URI string?

A URI is a string containing characters that identify a physical or logical resource. URI follows syntax rules to ensure uniformity. Moreover, it also maintains extensibility via a hierarchical naming scheme. The full form of URI is Uniform Resource Identifier.

Is a URI a type of URL?

URL. Although often used interchangeably, the "URI" and "URL" are different. A URI is an identifier of a specific resource while a URL is a special type of identifier that identifies a resource and specifies how it can be accessed.

What is a URI example?

A URI — short for “Uniform Resource Identifier” — is a sequence of characters that distinguishes one resource from another. For example, foo://example.com:8042/over/there?name=ferret#nose is a URI containing a scheme name, authority, path, query and fragment. A URI does not need to contain all these components.


1 Answers

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

like image 79
Tim B Avatar answered Oct 09 '22 07:10

Tim B