When an expression contains multiple operators, it can potentially be read in different ways. For example, the mathematical expression 1 + 2 x 3 could be read in two ways:
Add 1 and 2 and multiply the result by 3. This gives the answer 9. If we added parentheses, this would look like ( 1 + 2 ) x 3.
Add 1 to the result of multiplying 2 and 3. This gives the answer 7. If we added parentheses, this would look like 1 + ( 2 x 3 ).
In mathematics, the convention is to read the expression the second way. The general rule is that multiplication and division are done before addition and subtraction. When more advanced mathematical notation is used, either the meaning is either "self-evident" (to a trained mathematician!), or parentheses are added to disambiguate. In either case, the effectiveness of the notation to convey meaning depends on the intelligence and shared knowledge of the mathematicians.
Java has the same clear rules on how to read an expression, based on the precedence of the operators that are used.
In general, each operator is ascribed a precedence value; see the table below.
For example:
1 + 2 * 3
The precedence of + is lower than the precedence of *, so the result of the expression is 7, not 9.
Expressions in Java are the primary construct for doing calculations. Here are some examples:
1// A simple literal is an expression1+2// A simple expression that adds two numbers(i + j)/ k // An expression with multiple operations(flag)? c : d // An expression using the "conditional" operator(String) s // A type-cast is an expression
obj.test()// A method call is an expressionnewObject()// Creation of an object is an expressionnewint[]// Creation of an object is an expression
In general, an expression consists of the following forms:
Expression names which consist of:
- Simple identifiers; e.g. `someIdentifier`
- Qualified identifiers; e.g. `MyClass.someField`
Literals; e.g. 1, 1.0, 'X', "hello", false and null
Class literal expressions; e.g. MyClass.class
this and <TypeName> . this
Parenthesized expressions; e.g. ( a + b )
Class instance creation expressions; e.g. new MyClass(1, 2, 3)
Array instance creation expressions; e.g. new int[3]
Field access expressions; e.g. obj.someField or this.someField
Array access expressions; e.g. vector[21]
Method invocations; e.g. obj.doIt(1, 2, 3)
Method references (Java 8 and later); e.g. MyClass::doIt
The details of the different forms of expressions may be found in other Topics.
In most cases, an expression has a static type that can be determined at compile time by examining and its subexpressions. These are referred to as stand-alone expressions.
However, (in Java 8 and later) the following kinds of expressions may be poly expressions:
Parenthesized expressions
Class instance creation expressions
Method invocation expressions
Method reference expressions
Conditional expressions
Lambda expressions
When an expression is a poly expression, its type may be influenced by the expression's target type; i.e. what it is being used for.
The value of an Expression
The value of an expression is assignment compatible with its type. The exception to this is when heap pollution has occurred; e.g. because "unsafe conversion" warnings have been (inappropriately) suppressed or ignored.
Expression Statements
Unlike many other languages, Java does not generally allow expressions to be used as statements. For example:
publicvoidcompute(int i,int j){
i + j;// ERROR}
Since the result of evaluating an expression like cannot be use, and since it cannot affect the execution of the program in any other way, the Java designers took the position that such usage is either a mistake, or misguided.
However, this does not apply to all expressions. A subset of expressions are (in fact) legal as statements. The set comprises:
Assignment expression, including operation-and-becomes assignments.
Pre and post increment and decrement expressions.
Method calls (void or non-void).
Class instance creation expressions.
Constant Expressions
A constant expression is an expression that yields a primitive type or a String, and whose value can be evaluated at compile time to a literal. The expression must evaluate without throwing an exception, and it must be composed of only the following:
Primitive and String literals.
Type casts to primitive types or `String`.
The following unary operators: `+`, `-`, `~` and `!`.
The following binary operators: `*`, `/`, `%`, `+`, `-`, `<<`, `>>`, `>>>`, `<`, `<=`, `>`, `>=`, `==`, `!=`, `&`, `^`, `|`, `&&` and `||`.
The ternary conditional operator `?` `:`.
Parenthesized constant expressions.
Simple names that refer to constant variables. (A constant variable is a variable declared as `final` where the initializer expression is itself a constant expression.)
Qualified names of the form ` . ` that refer to constant variables.
Note that the above list excludes++ and --, the assignment operators, class and instanceof, method calls and references to general variables or fields.
Constant expressions of type String result in an "interned" String, and floating point operations in constant expressions are evaluated with FP-strict semantics.
Uses for Constant Expressions
Constant expressions can be used (just about) anywhere that a normal expression can be used. However, they have a special significance in the following contexts.
Constant expressions are required for case labels in switch statements. For example:
switch(someValue){case1+1:// OKcaseMath.min(2,3):// Error - not a constant expressiondoSomething();}
When the expression on the right hand side of an assignment is a constant expression, then the assignment can perform a primitive narrowing conversion. This is allowed provided that the value of the constant expression is within the range of the type on the left hand side. (See JLS 5.1.3(opens new window) and 5.2(opens new window)) For example:
byte b1 =1+1;// OK - primitive narrowing conversion.byte b2 =127+1;// Error - out of rangebyte b3 = b1 +1;// Error - not a constant expessionbyte b4 =(byte)(b1 +1);// OK
When a constant expression is used as the condition in a do, while or for, then it affects the readability analysis. For example:
while(false){doSomething();// Error - statenent not reachable}boolean flag =false;while(flag){doSomething();// OK}
(Note that this does not apply if statements. The Java compiler allows the then or else block of an if statement to be unreachable. This is the Java analog of conditional compilation in C and C++.)
Finally, static final fields in an class or interface with constant expression initializers are initialized eagerly. Thus, it is guaranteed that these constants will be observed in the initialized state, even when there is a cycle in the class initialization dependency graph.