Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Java URL Class
2.1.
Constructors of Java URL class
2.2.
Commonly Used Methods of the Java URL Class
2.3.
Example of Java URL class
3.
FAQs
4.
Key Takeaways
Last Updated: Mar 27, 2024

Java URL

Author Sanjana Yadav
0 upvote
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

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

  1. URL(String address) throws MalformedURLException- Generates a URL object from the given String.
     
  2. URL(String protocol, String host, String file)- Creates a URL object based on the given protocol, host, and file name parameters.
     
  3. URL(String protocol, String host, int port, String file)-  Creates a URL object using the protocol, host, port, and file name.
     
  4. URL(URL context, String spec)- Creates a URL object in the given context by parsing the provided spec.
     
  5. 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.
     
  6. 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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

FAQs

  1. What is the URL class in Java?
    The Java URL class serves as a portal to web resources. The java. net object. The URL class represents the URL, and this object controls all of the data in the URL string.
     
  2. What is the malformed exception in Java?
    When the built-in URL class encounters an invalid URL, a MalformedURLException is thrown; especially, when the protocol that is given is absent or invalid.
     
  3. Which method knows the host of the URL class?
    getHost() method
     
  4. How do I authenticate a URL in Java?
    The setAuthenticator(Authenticator auth) function is part of the Java HttpURLConnection class. This technique is used to initiate an authentication request using the HTTP protocol. Default authentication is used if no authentication is given.

Key Takeaways

  • Cheers if you reached here! In this blog, we learned about the Java URL class.
  • We have covered the basic introduction of this class and how it works.
  • We have also seen its constructor and several methods inside it.
  • Further, we saw an example to get a clear understanding of the topic.

On the other hand, learning never ceases, and there is always more to learn. So, keep learning and keep growing, ninjas!
If you wish to learn about more such interesting topics, you can visit Http URL In Java from coding ninjas.
Good luck with your preparation!

Live masterclass