URL Class in Java with Examples

18 Apr, 2022
URL known as Uniform Resource Locator is simply a string of text that identifies all the resources on the Internet, telling us the address of the resource, how to communicate with it, and retrieve something from it.
A URL can have many forms. The most general however follows a three-components system as proposed below:
Protocol: HTTP is the protocol hereHostname: Name of the machine on which the resource lives.File Name: The pathname to the file on the machine.Port Number: Port number to which to connect (typically optional).
Protocol: HTTP is the protocol here
Hostname: Name of the machine on which the resource lives.
File Name: The pathname to the file on the machine.
Port Number: Port number to which to connect (typically optional).
The URL class is the gateway to any of the resources available on the internet. A Class URL represents a Uniform Resource Locator, which is a pointer to a “resource” on the World Wide Web. A resource can point to a simple file or directory, or it can refer to a more complicated object, such as a query to a database or to a search engine.
URL(String address) throws MalformedURLException: It creates a URL object from the specified String.URL(String protocol, String host, String file): Creates a URL object from the specified protocol, host, and file name.URL(String protocol, String host, int port, String file): Creates a URL object from protocol, host, port, and file name.URL(URL context, String spec): Creates a URL object by parsing the given spec in the given context. URL(String protocol, String host, int port, String file, URLStreamHandler handler):Creates a URL object from the specified protocol, host, port number, file, and handler.URL(URL context, String spec, URLStreamHandler handler):Creates a URL by parsing the given spec with the specified handler within a specified context.
URL(String address) throws MalformedURLException: It creates a URL object from the specified String.
URL(String protocol, String host, String file): Creates a URL object from the specified protocol, host, and file name.
URL(String protocol, String host, int port, String file): Creates a URL object from protocol, host, port, and file name.
URL(URL context, String spec): Creates a URL object by parsing the given spec in the given context. 
URL(String protocol, String host, int port, String file, URLStreamHandler handler):Creates a URL object from the specified protocol, host, port number, file, and handler.
URL(URL context, String spec, URLStreamHandler handler):Creates a URL by parsing the given spec with the specified handler within a specified context.
Example:
Java
// Java program to demonstrate working of URL  // Importing required classesimport java.net.MalformedURLException;import java.net.URL;  // Main class// URL classpublic class GFG {      // Main driver method    public static void main(String[] args)        throws MalformedURLException    {          // Creating a URL with string representation        URL url1 = new URL(            "https://www.google.co.in/?gfe_rd=cr&ei=ptYq"            + "WK26I4fT8gfth6CACg#q=geeks+for+geeks+java");          // Creating a URL with a protocol,hostname,and path        URL url2 = new URL("http", "www.geeksforgeeks.org",                           "/jvm-works-jvm-architecture/");          URL url3 = new URL(            "https://www.google.co.in/search?"            + "q=gnu&rlz=1C1CHZL_enIN71"            + "4IN715&oq=gnu&aqs=chrome..69i57j6"            + "9i60l5.653j0j7&sourceid=chrome&ie=UTF"            + "-8#q=geeks+for+geeks+java");          // Printing the string representation of the URL        System.out.println(url1.toString());        System.out.println(url2.toString());        System.out.println();        System.out.println(            "Different components of the URL3-");          // Retrieving the protocol for the URL        System.out.println("Protocol:- "                           + url3.getProtocol());          // Retrieving the hostname of the url        System.out.println("Hostname:- " + url3.getHost());          // Retrieving the default port        System.out.println("Default port:- "                           + url3.getDefaultPort());          // Retrieving the query part of URL        System.out.println("Query:- " + url3.getQuery());          // Retrieving the path of URL        System.out.println("Path:- " + url3.getPath());          // Retrieving the file name        System.out.println("File:- " + url3.getFile());          // Retrieving the reference        System.out.println("Reference:- " + url3.getRef());    }}
Output:  
https://www.google.co.in/?gfe_rd=cr&ei=ptYqWK26I4fT8gfth6CACg#q=geeks+for+geeks+java
https://www.geeksforgeeks.org/jvm-works-jvm-architecture/

Different components of the URL3-
Protocol:- https
Hostname:- www.google.co.in
Default port:- 443
Query:- q=gnu&rlz=1C1CHZL_enIN714IN715&oq=gnu&aqs=chrome..69i57j69i60l5.653j0j7&sourceid=chrome&ie=UTF-8
Path:- /search
File:- /search?q=gnu&rlz=1C1CHZL_enIN714IN715&oq=gnu&aqs=chrome..69i57j69i60l5.653j0j7&sourceid=chrome&ie=UTF-8
Reference:- q=geeks+for+geeks+java
This article is contributed by Rishabh Mahrsee. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
Akanksha_Rai
akshaysingh98088
sumitgumber28
solankimayank
Java-Classes
Java-URL
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
How to iterate any Map in Java
Interfaces in Java
HashMap in Java with Examples
Stream In Java
ArrayList in Java
Collections in Java
Singleton Class in Java
Multidimensional Arrays in Java
Set in Java