Learn Java URL Class With Examples

Stay updated with the latest technology trends while you're on the move - Join DataFlair's Telegram Channel

1. Objective

In our last Tutorial, we discuss Pair Class in Java. Here, in this Java tutorial, we are going to learn about the Java URL class, which we will use as a gateway to the internet. Moreover, we will study what is URL and various segments of the URL. At last, we will discuss Java URL Class Example to understand better.

So, let us start Java URL Class.

Learn Java URL Class With Examples

Learn Java URL Class With Examples

2. What is Java URL Class?

Java URL class is the entryway to any of the asset accessible the on web. A Class URL speaks to a Uniform Resource Locator, which is a pointer to an “asset” on the World Wide Web. An asset can point to a straightforward document or catalog, or it can allude to a more convoluted question, for example, an inquiry to a database or to a web search tool.

Do you know What is the Switch Statement in Java?

3. What is a URL?

The same number of you should realize that Uniform Resource Locator-URL is a string of content that distinguishes every one of the assets on Internet, revealing to us the address of the asset, how to speak with it and recover something from it.
A Simple URL looks like –

Example of URL -  Uniform Resource Locator

Example of URL –  Uniform Resource Locator

a. Segments of a URL

A URL can have numerous structures. The broadest, however, takes after a three-segment framework

Protocol: HTTP is the convention here

Let’s Explore Decision Making in Java Programming

Hostname: Name of the machine on which the asset lives.

Document Name: The way name to the record on the machine.

Port Number: Port number to which to interface (commonly discretionary).

b. Some Constructors for Java URL Class

Here, are some important Constructors explained for Java URL Class, let’s discuss them one by one:

  • URL(String address) throws MalformedURLException: It makes a URL protest from the predefined String.
  • URL(String protocol, String host, String file): Creates a URL question from the predetermined protcol, host, and record name.
  • URL(String protocol, String host, int port, String record): Creates a URL question from convention, host, port and document name.
  • URL(URL content, String spec): Creates a URL question by parsing the given spec in the given setting.
  • URL(String protocol, String host, int port, String record, URLStreamHandler handler): Makes a URL question from the predetermined convention, have, port number, document, and handler.
  • URL(URL context, String spec, URLStreamHandler handler): Makes a URL by parsing the given spec with the predefined handler inside a predetermined setting.

Let’s Read about Method Overloading vs Overriding in Java

c. Java URL Class Example

import java.net.MalformedURLException;
import java.net.URL;
public class URLclass1
{
    public static void main(String[] args)
                 throws MalformedURLException
   {
        URL url1 =
        new URL("https://www.google.co.in/?gfe_rd=cr&ei=ptYq" +
               "WK26I4fT8gfth6CACg#q=data+flair+java");
        URL url2 = new URL("http", "www.data-flair.com",
                         "/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=data+flair+java");
        System.out.println(url1.toString());
        System.out.println(url2.toString());
        System.out.println();
        System.out.println("Different components of the URL3-");
        System.out.println("Protocol:- " + url3.getProtocol());
        System.out.println("Hostname:- " + url3.getHost());
        System.out.println("Default port:- " +
                                        url3.getDefaultPort());
        System.out.println("Query:- " + url3.getQuery());
        System.out.println("Path:- " + url3.getPath());
        System.out.println("File:- " + url3.getFile());
        System.out.println("Reference:- " + url3.getRef());
    }
}

Explanation of some methods used in the above program is as follows:

  • Public String toString(): As in any class, toString() restores the string portrayal of the given URL protest.
  • Public String getAuthority(): restores the specialist part of the URL or invalid if discharge.
  • Public String getPath(): restores the way of the URL, or invalid if purge.
  • Public String getQuery(): restores the inquiry part of the URL. An inquiry is a part after the ‘?’ in the URL. At whatever point rationale is utilized to show the outcome, there would be an inquiry field in URL. It is like questioning a database.
  • Public String getHost(): restore the hostname of the URL in IPv6 arrange.
  • Public String getFile(): restores the record name.
  • Public String getRef(): Returns the reference of the URL question. Generally, the reference is the part set apart by a ‘#’ in the URL. You can see the working example by questioning anything on google and seeing the part after ‘#’.
  • Public int getPort(): restores the port-related with the convention indicated by the URL.
  • Public int getDefaultPort: restores the default port utilized.
  • Public String getProtocol(): restores the convention utilized by the URL.

Read about Linear Data Structures in Java Programming Language

So, this was all about Java URL Class Tutorial. Hope you like our explanation.

Java Quiz

4.  Conclusion

Hence, in this tutorial, we learned about the Java URL class in Java and the Basic URL we used on the internet. In conclusion, we studied segments and constructs used in Java URL Class with an example. Furthermore, if you have any query feel free to ask in the comment section.

Related Topic – Java Generics 

For reference

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.