Introduction
As many of you are probably aware; a Uniform Resource Locator-URL is a string of text that identifies all resources on the Internet, informing us of the address of the resource, how to connect with it, and how to obtain anything from it.
A simple example of url is

Components of a URL:
A URL can take several different formats. The most common, however, is a three-component system.
Protocol: HTTP is the protocol used in this case.
Hostname: The name of the computer where the resource is stored.
File Name: The file's path name on the computer.
Port Number: The port number to which you want to connect (typically optional).
Must read, Multithreading in java, Duck Number in Java and Hashcode Method in Java
Java URL Class
The URL class serves as a portal to any resource available on the internet. A Class URL is a Uniform Resource Locator (URL), which refers to a "resource" on the World Wide Web. A resource might be a simple file or directory or a more complex object, such as a query to a database or a search engine.
Constructors of Java URL class
-
URL(String address) throws MalformedURLException- Generates a URL object from the given String.
-
URL(String protocol, String host, String file)- Creates a URL object based on the given protocol, host, and file name parameters.
-
URL(String protocol, String host, int port, String file)- Creates a URL object using the protocol, host, port, and file name.
-
URL(URL context, String spec)- Creates a URL object in the given context by parsing the provided spec.
-
URL(String protocol, String host, int port, String file, URLStreamHandler handler)- Creates a URL object based on the given protocol, host, port number, file, and handler parameters.
- URL(URL context, String spec, URLStreamHandler handler)- Creates a URL by parsing the provided spec with the chosen handler in the context specified.
Commonly Used Methods of the Java URL Class

Example of Java URL class
import java.net.MalformedURLException;
import java.net.URL;
public class URLClassDemo {
public static void main(String[] args) {
try {
URL url = new URL(
"https://www.google.com/search?q=switch+case+in+python+coding+ninjas&rlz=1C1CHBF_enIN980IN980&oq=&aqs=chrome.0.35i39i362l8.3955668j0j15&sourceid=chrome&ie=UTF-8");
System.out.println("Protocol: " + url.getProtocol());
System.out.println("Host Name: " + url.getHost());
System.out.println("Port Number: " + url.getPort());
System.out.println("Default Port Number: " + url.getDefaultPort());
System.out.println("Query String: " + url.getQuery());
System.out.println("Path: " + url.getPath());
System.out.println("File: " + url.getFile());
} catch (Exception e) {
System.out.println(e);
}
}
}
Output
Protocol: https
Host Name: www.google.com
Port Number: -1
Default Port Number: 443
Query String: q=switch+case+in+python+coding+ninjas&rlz=1C1CHBF_enIN980IN980&oq=&aqs=chrome.0.35i39i362l8.3955668j0j15&sourceid=chrome&ie=UTF-8
Path: /search
File: /search?q=switch+case+in+python+coding+ninjas&rlz=1C1CHBF_enIN980IN980&oq=&aqs=chrome.0.35i39i362l8.3955668j0j15&sourceid=chrome&ie=
UTF-8
Practice by yourself on java online compiler.