Expressions
An expression is a construct made up of variables, operators, and method invocations, which are constructed according to the syntax of the language, that evaluates to a single value.
int cadence = 0;
The data type of the value returned by an expression depends on the elements used in the expression. The expression cadence = 0 returns an int because the assignment operator returns a value of the same data type as its left-hand operand; in this case, cadence is an int.
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.
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.
operator group | associativity | type of operation |
---|---|---|
! ~ ++ -- + - | right-to-left | unary |
* / % | left-to-right | multiplicative |
+ - | left-to-right | additive |
<< >> >>> | left-to-right | bitwise |
< <= > >= | left-to-right | comparison |
== != | left-to-right | comparison |
& | left-to-right | bitwise |
^ | left-to-right | bitwise |
| | left-to-right | bitwise |
&& | left-to-right | boolean |
|| | left-to-right | boolean |
?: | right-to-left | conditional |
= += -= *= /= %= &= ^= |= <<= >>= >>>= |
right-to-left | assignment |
, | left-to-right | comma |
Subexpression | Matches |
---|---|
^ | Matches beginning of line. |
$ | Matches end of line. |
. | Matches any single character except newline. Using m option allows it to match newline as well. |
[...] | Matches any single character in brackets. |
[^...] | Matches any single character not in brackets |
\A | Beginning of entire string |
\z | End of entire string |
\Z | End of entire string except allowable final line terminator. |
re* | Matches 0 or more occurrences of preceding expression. |
re+ | Matches 1 or more of the previous thing |
re? | Matches 0 or 1 occurrence of preceding expression. |
re{ n} | Matches exactly n number of occurrences of preceding expression. |
re{ n,} | Matches n or more occurrences of preceding expression. |
re{ n, m} | Matches at least n and at most m occurrences of preceding expression. |
a| b | Matches either a or b. |
(re) | Groups regular expressions and remembers matched text. |
(?: re) | Groups regular expressions without remembering matched text. |
(?> re) | Matches independent pattern without backtracking. |
\w | Matches word characters. |
\W | Matches nonword characters. |
\s | Matches whitespace. Equivalent to [\t\n\r\f]. |
\S | Matches nonwhitespace. |
\d | Matches digits. Equivalent to [0-9]. |
\D | Matches nondigits. |
\A | Matches beginning of string. |
\Z | Matches end of string. If a newline exists, it matches just before newline. |
\z | Matches end of string. |
\G | Matches point where last match finished. |
\n | Back-reference to capture group number "n" |
\b | Matches word boundaries when outside brackets. Matches backspace (0x08) when inside brackets. |
\B | Matches nonword boundaries. |
\n, \t, etc. | Matches newlines, carriage returns, tabs, etc. |
\Q | Escape (quote) all characters up to \E |
\E | Ends quoting begun with \Q |
Operator | Description | Example |
---|---|---|
+ | Addition - Adds values on either side of the operator | A + B will give 30 |
- | Subtraction - Subtracts right hand operand from left hand operand | A - B will give -10 |
* | Multiplication - Multiplies values on either side of the operator | A * B will give 200 |
/ | Division - Divides left hand operand by right hand operand | B / A will give 2 |
% | Modulus - Divides left hand operand by right hand operand and returns remainder | B % A will give 0 |
++ | Increment - Increases the value of operand by 1 | B++ gives 21 |
-- | Decrement - Decreases the value of operand by 1 | B-- gives 19 |
Operator | Description | Example |
---|---|---|
== | Checks if the values of two operands are equal or not, if yes then condition becomes true. | (A == B) is not true. |
!= | Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. | (A != B) is true. |
> | Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. | (A > B) is not true. |
< | Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. | (A < B) is true. |
>= | Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. | (A >= B) is not true. |
<= | Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. | (A <= B) is true. |
Operator | Description | Example |
---|---|---|
& | Binary AND Operator copies a bit to the result if it exists in both operands. | (A & B) will give 12 which is 0000 1100 |
| | Binary OR Operator copies a bit if it exists in either operand. | (A | B) will give 61 which is 0011 1101 |
^ | Binary XOR Operator copies the bit if it is set in one operand but not both. | (A ^ B) will give 49 which is 0011 0001 |
~ | Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. | (~A ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number. |
<< | Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. | A << 2 will give 240 which is 1111 0000 |
>> | Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. | A >> 2 will give 15 which is 1111 |
>>> | Shift right zero fill operator. The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros. | A >>>2 will give 15 which is 0000 1111 |