x = 7is an expression that assigns x the value seven. This expression itself evaluates to seven. Such expressions use assignment operators. On the other hand, the expression
(condition) ? val1 : val2If 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. For example,
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.
|x += y||x = x + y|
|x -= y||x = x - y|
|x *= y||x = x * y|
|x /= y||x = x / y|
|x %= y||x = x % y|
|x <<= y||x = x << y|
|x >>= y||x = x >> y|
|x >>>= y||x = x >>> y|
|x &= y||x = x & y|
|x ^= y||x = x ^ y|
|x |= y||x = x | y|
A comparison operator compares its operands and returns a logical value based on whether the comparison is true or not. The operands can be numerical or string values. When used on string values, the comparisons are based on the standard lexicographical ordering. They are described in the following table.
|Equal (= =)||Returns true if the operands are equal.||x == y returns true if x equals y.|
|Not equal (!=)||Returns true if the operands are not equal.||x != y returns true if x is not equal to y.|
|Greater than (>)||Returns true if left operand is greater than right operand.||x > y returns true if x is greater than y.|
|Greater than or equal (>=)||Returns true if left operand is greater than or equal to right operand.||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.||x < y returns true if x is less than y.|
|Less than or equal (<=)||Returns true if left operand is less than or equal to right operand.||x <= y returns true if x is less than or equal to y.|
operand1 operator operand2
A unary operator requires a single operand, either before or after the operator:
Arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value. The standard arithmetic operators are addition (+), subtraction (-), multiplication (*), and division (/). These operators work as they do in other programming languages.
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 preceding statement, 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 three, then the statement
y = x++ sets y to three
and increments x to four. If x is three, then the statement
y = ++x increments x to four and sets y to four.
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 three, then the statement
y = x-- sets y to three and decrements x to two. If x is three, then the statement
y = --x decrements x to two and sets y to two.
The unary negation precedes its operand and negates it. For example,
x = -x negates the value of x; that is, if x were three, it would become -3.
|Bitwise AND||a & b||Returns a one in each bit position if bits of both operands are ones.|
|Bitwise OR||a | b||Returns a one in a bit if bits of either operand is one.|
|Bitwise XOR||a ^ b||Returns a one in a bit position if bits of one but not both operands are one.|
|Bitwise NOT||~ a||Flips the bits of its operand.|
|Left shift||a << b||Shifts a in binary representation b bits to left, shifting in zeros from the right.|
|Sign-propagating right shift||a >> b||Shifts a in binary representation b bits to right, discarding bits shifted off.|
|Zero-fill right shift||a >>> b||Shifts a in binary representation b bits to the right, discarding bits shifted off, and shifting in zeros from the left.|
Bitwise logical operators
The bitwise logical operators work conceptually as follows:
|and (&&)||expr1 && expr2||Returns true if both logical expressions expr1 and expr2 are true. Otherwise, returns false.|
|or (||)||expr1 || expr2||Returns true if either logical expression expr1 or expr2 is true. If both are false, returns false.|
|not (!)||!expr||If expr is true, returns false; if expr is false, returns true.|
As logical expressions are evaluated left to right, they are tested for possible "short-circuit" evaluation using the following rules:
"my " + "string"returns the string
"my string".The shorthand assignment operator += can also be used to concatenate strings. For example, if the variable mystring has the value "alpha," then the expression
mystring += "bet"evaluates to "alphabet" and assigns this value to mystring.