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

conditional operator in case expression

XMLWordPrintable

    • generic
    • generic



      Name: laC46010 Date: 02/08/99



      The following example shows that javac (ver. 1.1.x, 1.2fcs, 1.2.2) allows
      usage of expressions which are not "constant expressions" as stated in JLS,
      notwithstanding that the value of some expressions can be known at the compile
      time.
      In the following example a conditional expression with local variable is used
      in switch statement. According to the JLS 15.27 a non-final variable can not be
      used in constant expression:

      "A compile-time constant expression is an expression denoting a value of
       primitive type or a String that is composed using only the following:

           Literals of primitive type and literals of type String
           Casts to primitive types and casts to type String
           The unary operators +, -, ~, and ! (but not ++ or --)
           The multiplicative operators *, /, and %
           The additive operators + and -
           The shift operators <<, >>, and >>>
           The relational operators <, <=, >, and >= (but not instanceof)
           The equality operators == and !=
           The bitwise and logical operators &, ^, and |
           The conditional-and operator && and the conditional-or operator ||
           The ternary conditional operator ? :
           Simple names that refer to final variables whose initializers are constant expressions
           Qualified names of the form TypeName . Identifier that refer to final variables
           whose initializers are constant expressions"


      Test example and javac diagnostics follow:
      -------------------------------------------------------
      novo35% cat test.java
      class test {
          test() {
              final boolean b = true;
              int i = 0;
              switch(0) {
              case b ? 0 : i:
              }
          }
      }
      novo35% java -version
      java version "1.2.2"
      Classic VM (build JDK-1.2.2-E, green threads, sunwjit)
      novo35% javac test.java
      novo35%
      -------------------------------------------------------

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

            wmaddoxsunw William Maddox (Inactive)
            leosunw Leo Leo (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: