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

java.util.Calendar.hashCode() returns different results on the equal objects

XMLWordPrintable

    • 1.2fcs
    • sparc
    • solaris_2.5
    • Verified



      Name: dfC67450 Date: 05/08/98



      java.util.Calendar.hashCode() returns different results on the equal objects.
      This method should return the same values on equal objects.
      JLS 1.0 item 20.1.4 says:

          If two objects are equal according to the equals method (20.1.3),
          then calling the hashCode method on each of the two objects must
          produce the same integer result.

      Here is the test demonstrating the bug:

      -----------------Test.java------------------------
      import java.util.*;

      public class Test {
          public static void main (String args[]){
              Calendar c1 = new CalendarTest();
              Calendar c2 = new CalendarTest();
              int hc1 = c1.hashCode();
              int hc2 = c2.hashCode();
              if (c1.equals(c2) & hc1 != hc2) {
                System.out.println("Test failed");
                System.out.println(" hashCode1: " + hc1);
                System.out.println(" hashCode2: " + hc2);
              } else {
                System.out.println("Test passed");
              }
          }
        
      }

      class CalendarTest extends Calendar {

                            // Implementation of abstract class Calendar

          public void computeTime() {}
          public void computeFields() {}
          public void add(int field, int amount) {}
          public void roll(int field, boolean up) {}
          public int getMinimum(int field) {return 0;}
          public int getMaximum(int field) {return 0;}
          public int getGreatestMinimum(int field) {return 0;}
          public int getLeastMaximum(int field) {return 0;}
      }
      ---------Output from the test---------------------
      Test failed
        hashCode1: -1089839614
        hashCode2: -709468670
      -------------------------------------------------

      ======================================================================

            aliusunw Alan Liu (Inactive)
            dfazunensunw Dmitri Fazunenko (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: