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

Compiler does not follow JLS section 4.2.2 when folding constant expressions

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 1.3.0
    • tools
    • x86
    • windows_98



      Name: tb29552 Date: 01/10/2001


      C:\try>java -version
      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

      /*
       * java.util.Date.getTime() returns a long, hence this entire
       * expression should be widened and evaluated as long:
       * "d1.getTime() + 1*365*24*60*60*1000"
       * Reference section 4.2.2 "Integer Operations" of the JLS
       * http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#51035
       */
      import java.util.Date;
      class LongVersusInt {
          public static void main(String[] args){

      Date d1 = new Date(-2208960000000L);//Mon Jan 01 08:00:00 GMT 1900

      Date d2 = new Date( d1.getTime() + 1*365*24*60*60*1000);
      // After silently overflowing, javac reduces this
      // to: new Date( d1.getTime() + 1471228928);

      /*
      * Addition of the "L" suffix forces the entire expression to be
      * widened:
      */
      Date d3 = new Date( d1.getTime() + 1*365*24*60*60*1000L);
      // javac reduces to: new Date( d1.getTime() + 31536000000L);

      System.out.println(d1);
      System.out.println(d2);
      System.out.println(d3);
          }
      }

      (Review ID: 114615)
      ======================================================================

            gafter Neal Gafter (Inactive)
            tbell Tim Bell
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: