Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java List Interface

Java Queue Interface

Java Map Interface

Java Set Interface

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Useful Resources

Java - URL equals() Method with Examples



Description

The Java URL equals(E e) method compares the current URL object for equality with another object based on following criteria −

  • Return false if the given object is not a URL.

  • Two URL objects are equal if they both are having the same protocol, reference equivalent hosts, same port number on the host, and the same file and fragment of the file.

  • Two hosts are considered equivalent if both host names can be resolved into the same IP addresses.

  • If either host name can't be resolved, the host names must be equal without regard to case.

  • Both host names equal to null then return true.

Since hosts comparison requires name resolution, this operation is a blocking operation.

Declaration

Following is the declaration for java.net.URL.equals(Object obj) method

public boolean equals(Object obj)

Parameters

obj − The URL object to be compared.

Return Value

true if the objects are the same; false otherwise.

Exception

NA

Example 1

The following example shows the usage of Java URL equals(Object e) method. In this example, we're creating an instances of URL and URLConnection classes. Now using equals method we're comparing both the objects. As being different type of objects, result is false as expected and verified in output as shown below −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

public class UrlDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://www.tutorialspoint.com/index.htm?language=en#j2se");
         URLConnection urlConnection = url.openConnection();
         System.out.println(url.equals(urlConnection));
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

false

Example 2

The following example shows the usage of Java URL equals(Object e) method. In this example, we're creating two instances of URL classes with same url. Now using equals method we're comparing both the objects. As being object based on same url, result is as expected and verified in output as shown below −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;

public class UrlDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://www.tutorialspoint.com/index.htm?language=en#j2se");
         URL urlToCompare = new URL("https://www.tutorialspoint.com/index.htm?language=en#j2se");
         System.out.println(url.equals(urlToCompare));
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

true

Example 3

The following example shows the usage of Java URL equals(Object e) method. In this example, we're creating two instances of URL classes with same url and in another instance, we're also passing host and file name. Now using equals method we're comparing both the objects. As being object are not same now, result is as expected and verified in output as shown below −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;

public class UrlDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://www.tutorialspoint.com/index.htm?language=en#j2se");
         URL urlToCompare = new URL("https", "https://www.tutorialspoint.com/index.htm?language=en#j2se","index.htm" );
         System.out.println(url.equals(urlToCompare));
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

false
java_url.htm
Advertisements