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.
(=, +=, -=, *=, /=)
=
), 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:
operator operand
or
operand operator
For example x++
or ++x
.
+
-
*
/
These operators work in the standard way.
%
)
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 binary bitwise operators work conceptually as follows:
Shift operators convert their operands to 32-bit integers and return a result of the same type as the left operator.
<
)
Example TBD.
>>
)
Example TBD.
>>>
)
Example TBD.
++
)
var++
or ++var
The increment operator does two things: It increments its operand, and it
returns a value. If used postfix (for example, x++
), then it
returns the value before incrementing. If used prefix (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.
--
)
var--
or --var
The decrement operator does two things: It decrements its operand, and it
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.
-
)
x = -x
negates the value of x; that is, if x were 3, it would become -3.
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.
A complex logical expression involving the || operator is always tested for
"short circuit" evaluation. What this means is that if one of the operands
evaluates immediately to true
, and the other is still undetermined
(because it requires further evaluation), the expression immediately evaluates
to true, since it doesn't matter what the undetermined part is; anything OR
true
is true.
!
)
!expr
The logical "not" operator negates its operand expression
expr
. That is, if expr
is true, it returns false, and
if expr
is false, then it returns true.
false
: &&
anything is short-circuit
evaluated to false
.
true
: || anything is short-circuit evaluated to
true
.
This is possible because anything AND false
is always false, and
any value OR true
is always true.
= =, >, >=, <, <=, !=
)
The operators are:
= =
): returns true if the operands are equal.
!=
): returns true if the left operand is not equal
to the right operand.
>
): returns true if the left operand is
greater than the right operand. Example: x >
y returns true if
x is greater than y.
>=
): returns true if left operand
is greater than or equal to right operand. Example: x >= y
.
<
): returns true if left operand is less than
right operand. Example: x < y
.
<=
): returns true if left operand is
less than or equal to right operand. Example: x <= y
.
+
) concatenates two
string values together, returning another string that is the union of the two
operand strings. For example,
"my " + "string"
returns the string
"my 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 += "two"
evaluates to "onetwo" and assigns this value to mystring
.
The precedence of operators, from highest to lowest is as follows:
,
= += -= *= /= %= <<= >>= >>>= &=
^= |=
?:
||
&&
|
^
&
== !=
< <= > >=
<<>> >>>
+ -
* / %
! ~ - ++ --
() [] .