Table of contents
1.
Introduction
2.
URL
2.1.
Important Components of a URL:
3.
URL class
3.1.
Constructors of URL Class
3.2.
Example
4.
Frequently Asked Questions
5.
Conclusion 
Last Updated: Mar 27, 2024

URL class and method in Java

Author Pradeep Kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, we will look into the URL class of Java. An URL can be used to uniquely access any resource on the World Wide Web. We will look at the different components of a URL and their respective uses. 
If you are new to Java and are yet to write your first program in the language, you can check out this article to get started with the language.

URL

URL (Uniform Resource Locator) is the address of a given unique resource on the Web. By using a URL, you can uniquely identify and access any resource on the internet. A URL can point to any resource on the Web, such as an HTML page, a CSS Document, an image file, a document file, etc. It can also refer to an API endpoint that queries a specific database and returns the response. 

Here are some examples of URLs:

https://www.codingninjas.com
https://www.codingninjas.com/courses/android-app-development

Important Components of a URL:

In most cases, an HTTP (or HTTPS) URL consists of three or four components:

  • Scheme: It specifies the protocol that will be used to access the specified Internet resource. For example, HTTP or HTTPS. 
  • Host: It is the name of the machine on which the resource is situated. For example, www.codingninjas.com.
  • Path: It specifies the name of the specified resource that the user wants to access on the given host. For example: /courses/android-app-development.
  • Port Number: It specifies the port of the host to which the user must connect in order to access the requested resource.
     

Also see, Duck Number in Java and Hashcode Method in Java

URL class

The URL Class in Java is used to represent an URL (Uniform Resource Locator). A class URL points to a unique resource on the World Wide Web. For example, https://www.codingninjas.com.

URL class provides us with various constructors to create an URL.

Constructors of URL Class

Following are the different types of constructors that the URL Class provides:

  1. URL(String address): It takes the address of the resource as input and returns a URL object.
  2. URL(URL context, String spec): By parsing the given spec inside a certain context, this method creates an object of a URL.
  3. URL(String protocol, String host, int port, String file, URLStreamHandler handler): It takes the protocol, host, port number, file, and URLStreamHandler of the resource and returns a URL object.
  4. URL(String protocol, String host, String file): Using this constructor, you can define the protocol (HTTP or HTTPS), host, and file (i.e., the path of the resource on the host).
  5. URL(String protocol, String host, int port, String file): It is very similar to the previous constructor. Additionally, you can mention the port number of the resource.
  6. URL(URL context, String spec, URLStreamHandler handler): Creates a URL by processing the supplied specification with the defined handler in a particular context.

Example

Now that we've covered the fundamentals of URL class, let's look at a sample Java program using URL class:

import java.net.MalformedURLException;
import java.net.URL;

// Main Class
class Main
{
   public static void main(String args[]) throws MalformedURLException {
        // Create a URL object using the address of the resource
        URL url_address = new URL("https://www.codingninjas.com/courses/android-app-development");
       
        // the above url can also be created like this
        var url_protocol_host_path = new URL("https","www.codingninjas.com","/courses/android-app-development");

        // Printing the protocol of this url
        System.out.println("Protocol of the URL: "+ url_protocol_host_path.getProtocol());
        // Printing the hostname of this url
        System.out.println("HostName of the URL: " + url_protocol_host_path.getHost());
       
        System.out.println("HostName of the URL: " + url_protocol_host_path.getPath());

        // creating a url using protocol, host, port, path
        var url_protocol_host_port_path = new URL("https","www.codingninjas.com",443,"/courses/android-app-development");
        // printing the port number of the url
        System.out.println("Port Number: " + url_protocol_host_port_path.getPort());
    }    
}

Output:

Protocol of the URL: https
HostName of the URL: www.codingninjas.com
HostName of the URL: /courses/android-app-development
Port Number: 443


Practice by yourself on java online compiler.

Frequently Asked Questions

  1. What is the distinction between HTTP and HTTPS?
    HTTPS is the more secured version of the HTTP protocol. It uses SSL to encrypt the normal HTTP request. 
  2. Why do we use MalformedURLException in a Java Program containing an object of the URL Class?
    In case any exception occurs while creating a URL class object, it will throw MalformedURLException. That's why we use that in the program.
  3. Is the port number a mandatory parameter to specify while creating an URL class Object?
    No, it is usually not required to mention the port number while accessing a URL. In case you are running a program on your local machine, you might need to specify the port number to access that resource.

Conclusion 

In this article, we have extensively discussed the URL class of JavaWe discussed how we could create an object of the URL Class using different types of constructors. Additionally, we looked at the different components of a URL Class.
We hope that this blog has helped you enhance your knowledge regarding the URL class, and if you like to learn more, check out our article on Reader Class. And to learn in-depth about android development, check out our Android Development course on the Coding Ninjas website. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass