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:
- URL(String address): It takes the address of the resource as input and returns a URL object.
- URL(URL context, String spec): By parsing the given spec inside a certain context, this method creates an object of a URL.
- 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.
- 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).
- 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.
- 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
-
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.
-
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.
-
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 Java. We 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!