- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between URI and URN in Java
The modern day Internet is all about the World Wide Web which holds billions of website and resources. There are several ways to access those web-based resources by following the protocols. Java has the concept of networking that is used to establish communication between clients and those resources. But, to locate a specific resource among the millions available, we need a unique identifier. There are three components: URI, URL and URN that helps us locate a certain resource on the web. This article aims to explain the difference between URI and URN in Java.
URI vs URN in Java
URI
It is an abbreviation of Uniform Resource Locator. It encompasses a sequence of characters that identifies a resource on the web. It defines a scheme such as http, ftp, mailto, and so forth that specifies how to access the resource. A URI enables the use of various resource identifiers within the same context, regardless of the differences in the methods used to access those resources. Java provides the URI class in ‘java.net’ package.
Syntax
URI nameOfObject = new URI( "URIspecifier" );
Here, URIspecifier is the actual URI or we can say link of the resource.
Instance
https://example.com/resource?id=12345&category=books
In the above example URI, we have a hypothetical domain 'example.com' followed by a resource path '/resource'. It also includes two query parameters named 'id' with a value of '12345' and 'category' with a value of 'books'.
URN
It is an acronym that stands for Uniform Resource Name. It defines the name of a certain resource. In other words, URN is a specific type of URI that identifies a resource by its name rather than its location. It has the scheme urn and a namespace identifier such as isbn and uuid that specifies the naming authority for the resource. Due to the unavailability of a universal standard for naming an object on the Internet, this method of identifying resources has failed.
Instance
urn:example:resource:books:12345
In the above example URN, we have used the 'example' namespace, followed by a resource identifier 'resource:books:12345'. The URN format consists of a hierarchical structure with different components separated by colons.
Example 1
In the following example, we will use a few in-built methods and classes of ‘java.net’ package to check the protocol used in a given URI.
import java.net.*; public class URIExample { public static void main(String args[]) throws Exception { try { // link of file URI uri = new URI("https://www.tutorialspoint.com/java/pdf/java_networking.pdf"); // creating URL object from the URI URL url = uri.toURL(); // making connection with the file URLConnection urlConn = url.openConnection(); // retrieving the name of protocol String scheme = uri.getScheme(); System.out.println("The name of protocol is: " + scheme); } catch(Exception exp) { System.out.println("Something went wrong!! Please check the link!!"); } } }
Output
The name of protocol is: https
In the above code, we have created an instance of URI class and passed a link of file to its constructor. Then, an instance of URL class is defined using URI so that we can make a connection with the given link. Using the getScheme() method we retrieved the name of the protocol used in the link.
Difference between URI and URN
The below table shows the differences between URI and URN −
URI |
URN |
---|---|
It can locate a resource available on the Internet. |
It can only name a resource. |
URI can be updated if the corresponding resource gets modified. |
It is persistent and does not depend on the location or name of the resource. |
It contains more information than URN. |
It contains lesser information than URI |
The URI consists of URN and URL. |
URN is the subset of URI. |
Conclusion
The URI is the superset of URN and URL. It can identify a resource by its name, location or both. On the other hand, URN is a subset of URI and identifies resources by their names within a given namespace.