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

Quirks in handling constant patterns

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: repo-amber
    • Component/s: tools
    • Labels:
      None
    • Subcomponent:
    • CPU:
      generic
    • OS:
      generic

      Description

      (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

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            sadayapalam Srikanth Adayapalam
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated: