Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8351848

URL.equals() incorrectly returns true for URLs with null hosts but different ports

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      There is a logical error in the URL.equals() method when comparing two URL instances that have null hosts but different port numbers. According to the documentation of java.net.URL class, two URL objects should be considered equal only when they meet multiple conditions, including having "the same port number on the host". However, when two URL objects both have null.



      ---------- BEGIN SOURCE ----------
      import java.net.URL;

      public class Main {
          public static void main(String[] args) throws Exception {
              URL urlNullHost1 = new URL("http", null, 80, "/Test");
              URL urlNullHost2= new URL("http", null, 8080, "/Test");
              
              if (urlNullHost1.equals(urlNullHost2)) {
                  throw new RuntimeException("Null hosts with different ports should not be equal");
              }
          }
      }
      ---------- END SOURCE ----------

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: