EXPRESSION

An expression is a syntactic construction that has a value. Expressions are formed by combining variables, constants, and method returned values using operators. Java expressions can have side-effects, which are actions that are executed when the expression is evaluated. In particular, an assignment expression is usually used for its side effect of assigning a new value to a variable. Method invokations also frequently have side effects.

Variables

The name of a variable by itself is an expression. The expression value is the current value of the variable.

Constants

The name of a constant by itself is an expression. A literal constant is also an expression. In either case, the expression value is the value of the constant.

Method Returned Values

A syntactically correct method invokation is an expression if the method does not have void return type. To be syntactically correct, a method invokation must have one argument for each parameter of the method. The arguments must be expressions separated by commas. The value of each expression must be convertable to the type declared for the corresponding parameter. The expression value of a method invokation is the value specified in the return statement of the method.

Operators, Precedence, and Associativity

Operators are tokens that are used to combine values in expressions. When evaluating an expression, Java uses precedence and associativity rules to determine the order in which operators are applied. These rules mostly are derived from the C language precedence and associativity rules.

In the table below, the Java operators are listed in order of precedence with highest precedence at the top. The rows of the table define precedence groups. Two operators in the same group have the same precedence. Higher precedence operators are applied before lower precedence operators. Except for bitwise operators, the precedence rules give Java expressions the meaning that you would normally expect from experience with mathematical expressions. If an expression behaves contrary to your intention, you can always force a different order of evaluation using parentheses.

Associativity rules are only used to determine the order of evaluation for two operators that are in the same group. Most groups use left-to-right associativity, which means that in an expression with operators in the same precedence group, the operators are applied in left-to-right order. This is what we normally expect for operators that appear in between two operands.

In normal mathematical notation, there are only a few cases where we expect associativity to be right-to-left. One is with unary operators that appear before their operand. Java does interpret such expressions according to our expectations, but it is rare that two Java unary operators are used consecutively. The other kind of operators where we normally expect right-to-left associativity is assignments. As usual, Java interprets assignments according to our expectations.

operator groupassociativitytype of operation
! ~ ++ -- + -right-to-leftunary
* / %left-to-rightmultiplicative
+ -left-to-rightadditive
<< >> >>>left-to-rightbitwise
< <= > >=left-to-rightcomparison
== !=left-to-rightcomparison
&left-to-rightbitwise
^left-to-rightbitwise
|left-to-rightbitwise
&&left-to-rightboolean
||left-to-rightboolean
?:right-to-leftconditional
= += -= *= /= %= &=
^= |= <<= >>= >>>=
right-to-leftassignment
,left-to-rightcomma

Assignment Expressions

An assignment expression has the following form.
    variable-expression assignment-operator expression

The variable expression can be just the name of a variable, or it can be an expression that selects a variable using array indices. The value type of the right-hand-side expression must be compatible with the variable type.

An assignment expression is most often used for its side effect: it changes the value of the variable selected by the variable expression to the value of the expression on the right-hand side. The value of the assignment expression is the value that is assigned to the selected variable.
In most common assignment expressions, the assignment operator is =. Then the assignment expression has the following form.
    variable-expression = expression

The Java arithmetic and bitwise operators can be combined with = to form assignment operators. For example, the += assignment operator indicates that the right-hand side should be added to the variable, and the *= assignment operator indicates that the right-hand side should be multiplied into the variable.

No comments:

Post a Comment