An expression is any valid set of literals, variables, operators, and expressions that evaluates to a single value. The value may be a number, a string, or a logical value. Conceptually, there are two types of expressions: those that assign a value to a variable, and those that simply have a value. For example, the expression
x = 7
is an expression that assigns x the value 7. This expression itself evaluates to 7. Such expressions use assignment operators. On the other hand, the expression
3 + 4
simply evaluates to 7; it does not perform an assignment. The operators used in such expressions are referred to simply as operators.
- Arithmetic: evaluates to a number, for example
- String: evaluates to a character string, for example "Fred" or "234"
- Logical: evaluates to true or false
The special keyword null denotes a null value. In contrast, variables that have not been assigned a value are undefined, and cannot be used without a run-time error.
A conditional expression can have one of two values based on a condition. The syntax is
(condition) ? val1 : val2
If condition is true, the expression has the value of val1, Otherwise it has the value of val2. You can use a conditional expression anywhere you would use a standard expression.
status = (age >= 18) ? "adult" : "minor"
This statement assigns the value "adult" to the variable status if age is eighteen or greater. Otherwise, it assigns the value "minor" to status.
Assignment Operators (=, +=, -=, *=, /=)
An assignment operator assigns a value to its left operand based on the value of its right operand. The basic assignment operator is equal (=), which assigns the value of its right operand to its left operand. That is, x = y assigns the value of y to x.
The other operators are shorthand for standard arithmetic operations as follows:
- x += y means x = x + y
- x -= y means x = x - y
- x *= y means x = x * y
- x /= y means x = x / y
- x %= y means x = x % y
There are additional assignment operators for bitwise operations:
- x <<= y means x = x << y
- x >>= y means x = x >> y
- x >>>= means x = x >>> y
- x &= means x = x & y
- x ^= means x = x ^ y
- x |= means x = x | y
operand1 operator operand2
For example, 3 + 4 or x * y
A unary operator requires a single operand, either before or after the operator:
For example x++ or ++x.
Arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value.
Standard Arithmetic Operators
The standard arthmetic operators are addition (+), subtraction (-), multiplication (*), and division (/). These operators work in the standard way.
The modulus operator is used as follows:
var1 % var2
The modulus operator returns the first operand modulo the second operand, that is, var1 modulo var2, in the statement above, where var1 and var2 are variables. The modulo function is the remainder of integrally dividing var1 by var2. For example, 12 % 5 returns 2.
The increment operator is used as follows:
This operator increments (adds one to) its operand and returns a value. If used postfix, with operator after operand (for example x++), then it returns the value before incrementing. If used prefix with operator before operand (for example, ++x), then it returns the value after incrementing.
For example, if x is 3, then the statement
y = x++
increments x to 4 and sets y to 3.
If x is 3, then the statement
y = ++x
increments x to 4 and sets y to 4.
The decrement operator is used as follows:
This operator decrements (subtracts one from) its operand and returns a value. If used postfix (for example x--) then it returns the value before decrementing. If used prefix (for example, --x), then it returns the value after decrementing.
For example, if x is 3, then the statement
y = x--
decrements x to 2 and sets y to 3.
If x is 3, then the statement
y = --x
decrements x to 2 and sets y to 2.
Unary negation (-)
The unary negation operator must precede its operand. It negates its operand. For example,
x = -x
negates the value of x; that is if x were 3, it would become -3.
Bitwise Logical Operators
The bitwise logical operators work conceptually as follows:
- The operands are converted to 32-bit integers, and expressed a series of bits (zeros and ones).
- Each bit in the first operand is paired with the corresponding bit in the second operand: first bit to first bit, second bit to second bit, and so on.
- The operator is applied to each pair of bits, and the result is constructed bitwise.
The bitwise operators are:
- Bitwise AND & returns a one if both operands are ones.
- Bitwise OR | returns a one if either operand is one.
- Bitwise XOR ^ returns a one if one but not both operands are one.
For example, the binary representation of 9 is 1001, and the binary representation of 15 is 1111. So, when the bitwise operators are applied to these values, the results are as follows:
- 15 & 9 yields 9 (1111 & 1001 = 1001)
- 15 | 9 yields 15 (1111 | 1001 = 1111)
- 15 ^ 9 yields 6 (1111 ^ 1001 = 0110)
Bitwise Shift Operators
The bitwise shift operators are:
- Left Shift (<<)
- Sign-propagating Right Shift (>>)
- Zero-fill Right shift (>>>)
The shift operators take two operands: the first is a quantity to be shifted, and the second specifies the number of bit positions by which the first operand is to be shifted. The direction of the shift operation is controlled by the operator used.
Shift operators convert their operands to 32-bit integers, and return a result of the same type as the left operator.
Left Shift (<<)
This operator shifts the first operand the specified number of bits to the left. Excess bits shifted off to the left are discarded. Zero bits are shifted in from the right.
For example, 9<<2 yields 36, because 1001 shifted two bits to the left becomes 100100, which is 36.
Sign-propagating Right Shift (>>)
This operator shifts the first operand the specified number of bits to the right. Excess bits shifted off to the right are discarded. Copies of the leftmost bit are shifted in from the left.
For example, 9>>2 yields 2, because 1001 shifted two bits to the right becomes 10, which is 2. Likewise, -9>>2 yields -3, because the sign is preserved.
Zero-fill right shift (>>>)
This operator shifts the first operand the specified number of bits to the left. Excess bits shifted off to the right are discarded. Zero bits are shifted in from the left.
For example, 19>>>2 yields 4, because 10011 shifted two bits to the right becomes 100, which is 4. For postive numbers, zero-fill right shift and sign-propagating right shift yield the same result.
Logical operators take logical (Boolean) values as operands. They return a logical value. Logical values are true and false.
expr1 && expr2
The logical "and" operator returns true if both logical expressions expr1 and expr2 are true. Otherwise, it returns false.
expr1 || expr2
The logical "or" operator returns true if either logical expression expr1 or expr2 is true. If both expr1 and expr2 are false, then it returns false.
The logical "not" operator is a unary operator that negates its operand expression expr. That is, if expr is true, it returns false, and if expr is false, then it returns true.
As logical expressions are evaluated left to right, they are tested for possible "short circuit" evaluation using the following rule:
- false && anything is short-circuit evaluated to false.
- true || anything is short-circuit evaluated to true.
The rules of logic guarantee that these evaluations will always be correct. Note that the anything part of the above expressions is not evaluated, so any side effects of doing so do not take effect.
Comparison Operators (= =, >, >=, <, <=, !=)
A comparison operator compares its operands and returns a logical value based on whether the comparison is true or not. The operands may be numerical or string values. When used on string values, the comparisons are based on the standard lexicographical ordering.
The operators are:
- Equal (= =): returns true if the operands are equal.
- Not equal (!=): returns true if the operands are not equal.
- Greater than (>): returns true if left operand is greater than right operand. Example: x > y returns true if x is greater than y.
- Greater than or equal to (>=): returns true if left operand is greater than or equal to right operand. Example: x >= y returns true if x is greater than or equal to y.
- Less than (<): returns true if left operand is less than right operand. Example: x < y returns true if x is less than y.
- Less than or equal to (<=): returns true if left operand is less than or equal to right operand. Example: x <= y returns true if x is less than or equal to y.
In addition to the comparison operators, which may be used on string values, the concatenation operator (+) concatenates two string values together, returning another string that is the union of the two operand strings. For example,
"my " + "string"
returns the string
The shorthand assignment operator += can also be used to concatenate strings. For example, if the variable mystring is a string that has the value "alpha", then the expression
mystring += "bet"
evaluates to "alphabet" and assigns this value to mystring.
The precedence of operators determines the order they are applied when evaluating an expression. You can override operator precedence by using parentheses.
The precedence of operators, from lowest to highest is as follows:
- comma ,
- assignment = += -= *= /= %= <<= >>= >>>= &= ^= |=
- conditional ?:
- logical-or ||
- logical-and &&
- bitwise-or |
- bitwise-xor ^
- bitwise-and &
- equality == !=
- relational < <= > >=
- bitwise shift << >> >>>
- addition/subtraction + -
- multiply/divide * / %
- negation/increment ! ~ - ++ --
- call, member ()  .