Skip to content

Java URL Class

Imagine you're on the internet, surfing away, and you come across a really cool website, like You know, one of those sites that just draws you in with its wealth of Java knowledge? Well, guess what? In Java, you can interact with URLs just like this one using something called a URL object.

So, what exactly is a URL? Think of it as the address of a website. Just like your home has a physical address, every website has a unique URL that tells your browser where to find it on the vast expanse of the internet.

Now, let's talk about Java and URLs. Java is a powerful programming language that allows you to do all sorts of cool stuff, including working with URLs. With Java, you can create, manipulate, and retrieve information from URLs effortlessly.

Using the URL as an example, let's say you want to fetch some data from this website using your Java program. Well, with Java's URL class, you can do just that! You can open a connection to the URL, read the data it returns, and then use that data however you like in your Java application.

But wait, there's more! Java URL handling isn't just limited to fetching data from websites. You can also use it to open connections to FTP servers, interact with files on your local machine, and much more. The possibilities are endless!

Let's break down each part of the URL :

  1. Protocol (https://): This part indicates the protocol being used to access the website. In this case, it's HTTPS, which stands for HyperText Transfer Protocol Secure. It's a secure version of HTTP, ensuring that the data transferred between your browser and the website is encrypted for privacy and security.

  2. Domain Name ( The domain name is the unique identifier for the website. It's like the address of a house on a street. In this URL, "" is the domain name. It typically represents a specific organization, service, or entity on the internet.

  3. Path (/java/url): The path specifies the specific location or resource within the website's server. It's like navigating through folders on a computer. In this URL, "/java/url" indicates that we're accessing a resource located within the "java" directory on the server, and specifically, the "url" file or directory within that.

  4. Port (if applicable): The port number, if specified, comes after the domain name and is separated by a colon. It's like a door number on a building, indicating which entrance to use when accessing the website. However, it's omitted in URLs, when using default ports like 80 for HTTP and 443 for HTTPS.

So, when you put it all together, is telling your browser to use the HTTPS protocol to securely connect to the "" website and access the resource located at the "/java/url" path.



public class URLExample {
    public static void main(String[] args) {
        try {
            // Create a URL object pointing to a web page
            URL url = new URL("");

            // Open a connection to the URL

            // Retrieve information about the URL
            System.out.println("Protocol: " + url.getProtocol());
            System.out.println("Host: " + url.getHost());
            System.out.println("Port: " + url.getPort());
            System.out.println("Path: " + url.getPath());
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());

In our example, we create a URL object pointing to the website "". Then, we open a connection to the URL and retrieve information about it, such as the protocol, host, port, and path.

Now, let's break down what's happening:

  • We import the necessary classes from the package to work with URLs.
  • Inside our main() method, we create a URL object representing the web page we want to access.
  • We then open a connection to the URL using the openConnection() method.
  • Finally, we retrieve and print information about the URL, such as its protocol, host, port, and path.

Waytojava is designed to make learning easier. We simplify examples for better understanding. We regularly check tutorials, references, and examples to correct errors, but it's important to remember that humans can make mistakes.