GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • HTTP
  • HTTPS
  • FTP
  • Introduction3 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Java URL Class - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

Java URL Class

Last Updated : 6 Oct, 2026

The URL class in the java.net package represents a Uniform Resource Locator, which identifies the location of a resource on the internet. It provides methods to access URL components and open a connection to the resource.

  • Provides access to components such as the protocol, host, port, path, and query.
  • Enables connections to resources through supported protocols, such as HTTP, HTTPS, and FTP.

Components of a URL

A URL can have many forms. The most general however follows a three-component system as proposed below:

URL-Class

Its main components are:

ComponentExampleDescription
Protocol/SchemehttpsSpecifies how the resource is accessed
Hostwww.example.comIdentifies the server that hosts the resource
Port443Identifies the network port used for communication
Path/products/searchIdentifies the resource location on the server
Queryq=javaContains parameters passed after ?
FragmentresultsIdentifies a specific section after

URL Class 

The URL class is a core Java networking class used to access internet-based resources.

  • It can point to static files, dynamic content, and APIs
  • Provides constructors to create URL objects
  • Offers methods to extract different parts of a URL

Constructors of URL class

1. URL(String address): It creates a URL object from the specified String.

URL url = new URL("https://www.example.com/");

2. URL(String protocol, String host, String file): Creates a URL object from the specified protocol, host, and file name.

URL url = new URL("http", "www.example.com", "/path/to/resource");

3. URL(String protocol, String host, int port, String file): Creates a URL object from protocol, host, port, and file name.

URL url = new URL("https", "www.example.com", 443, "/path/to/resource");

4. URL(URL context, String spec): Creates a URL object by parsing the given spec in the given context. 

URL baseUrl = new URL("https://www.example.com/");
URL relativeUrl = new URL(baseUrl, "/path/to/resource");

5. URL(String protocol, String host, int port, String file, URLStreamHandler handler): Creates a URL object from the specified protocol, host, port number, file, and handler.

URL url = new URL("http", "www.example.com", 80, "/path/to/resource", new MyCustomHandler());

6. URL(URL context, String spec, URLStreamHandler handler): Creates a URL by parsing the given spec with the specified handler within a specified context.

URL baseUrl = new URL("https://www.example.com/");
URL relativeUrl = new URL(baseUrl, "/path/to/resource", new MyCustomHandler());

Example: Program to demonstrate the working of URL.

Java
import java.net.URI;
import java.net.URL;

public class Geeks {

    public static void main(String[] args) throws Exception
    {

        URL u1
            = URI.create("https://www.google.co.in/?gfe_rd=cr&ei=ptYq"
                     + "WK26I4fT8gfth6CACg#q=geeks+for+geeks+java").toURL();

        URL u2 = URI.create("http://www.geeksforgeeks.org/"
                            + "jvm-works-jvm-architecture/")
                     .toURL();

        URL u3
            = URI.create("https://www.google.co.in/search"
                     + "?q=gnu&rlz=1C1CHZL_enIN714IN715"
                     + "&oq=gnu&sourceid=chrome"
                     + "&ie=UTF-8#q=geeks+for+geeks+java")
                  .toURL();

        System.out.println(u1);
        System.out.println(u2);

        System.out.println(
            "\nDifferent components of URL3:");

        System.out.println("Protocol: " + u3.getProtocol());

        System.out.println("Hostname: " + u3.getHost());

        System.out.println("Default port: "
                           + u3.getDefaultPort());

        System.out.println("Query: " + u3.getQuery());

        System.out.println("Path: " + u3.getPath());

        System.out.println("File: " + u3.getFile());

        System.out.println("Reference: " + u3.getRef());
    }
}

Output:

Output

Java URL Methods

MethodDescription
getAuthority()Returns the authority part of URL or null if empty
getDefaultPort()Returns the default port used
getFile()Returns the file name.
getHost()Return the hostname of the URL. If the host is a literal IPv6 address, it may be returned in bracketed IPv6 format.
getPath()Returns the path of the URL, or null if empty
getPort()Returns the port associated with the protocol specified by the URL
getProtocol()Returns the protocol used by the URL
getQuery()Return the query part of the URL, which is the portion following the ? character, used to pass parameters to a web application.
getRef()Return the reference part of the URL, which is the portion following the # character, typically used to navigate to a specific section within a web page.
toString()As in any class, toString() returns the string representation of the given URL object.
Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.