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

Quirks in handling constant patterns

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • repo-amber
    • None
    • tools
    • None
    • generic
    • generic

      (1) The following program does not compile:

      public class X {

          public static void main(String args[]) {
              Object o = null;
              if (o __matches (42 + 0)) {
              }
          }
      }

      /home/srikanth/tmp/X.java:5: error: ')' expected
              if (o __matches (42 + 0)) {
                   ^
      /home/srikanth/tmp/X.java:5: error: ';' expected
              if (o __matches (42 + 0)) {
                                      ^
      2 errors

      (2) The following program is expected to compile but fail at runtime with Arithmetic exception, but fails to compile:

      public class X {

          public static void main(String args[]) {
              Object o = null;
              if (o __matches 42 / 0) {
              }
          }
      }

      Javac message:
      /home/srikanth/tmp/X.java:5: error: constant expression required
              if (o __matches 42 / 0) {
                              ^
      1 error

      (Compare with int x = 42/0; which fails with Arithmetic exception)

      (3) This program shows some odd behavior:

      public class X {

          public static void main(String args[]) {
              Object o = null;
              if (o __matches String x __matches true) {} // This compiles
              if (o __matches true __matches false) {} // This does not - Why ?
          }
      }

      compiler's complaint being:
      /home/srikanth/tmp/X.java:6: error: constant expression required
              if (o __matches true __matches false) {} // This does not - Why ?
                              ^
      1 error

            Unassigned Unassigned
            sadayapalam Srikanth Adayapalam (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: