|
CHAPTER 15: Expressions |
|
 Previous |
 Java Language |
 Index |
 Next |
15.27 Constant Expression
ConstantExpression:
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
Compile-time constant expressions are used in case
labels in switch
statements (S14.9) and have a special significance for assignment conversion (S5.2).
Examples of constant expressions:
true
(short)(1*2*3*4*5*6)
Integer.MAX_VALUE / 2
2.0 * Math.PI
"The integer " + Long.MAX_VALUE + " is mighty big."
 | © 1996 Sun Microsystems, Inc. All rights reserved. |