Operators
Increment and Decrement Operators
Increment and decrement operators can be applied to all
integers and floating point types. They can be used either prefix
(--x, ++x) or postfix (x--, x++) mode.
Prefix Increment Operation
int x = 2;
int y = ++x; // x == 3, y == 3
Postfix Increment Operation
int x = 2;
int y = x++; // x == 3, y == 2
Type Conversions
Conversion from a lower range type to higher range type is
called widening and is done automatically. However,
to go from a higer range type to lower range type (narrowing)
you have to explicitly use type casting.
float x = 5.67f;
int i = (int) x;
Character Data Type
Java uses 16-bit Unicode to represent characters. The Unicode
representation is of the form '\uxxxx', where xxxx
is hexadecimal notation and ranges from 0000 to FFFF.
char ch = 'A';
char alpha = '\u03b1';
Boolean Data Type
Boolean data types can have two values either true
or false.
Comparison Operators
less than | < |
less than or equal to | <= |
greater than | > |
greater than or equal to | >= |
equal to | == |
not equal to | != |
Boolean or Logical Operators
NOT | ! |
AND | && |
OR | || |
EXCLUSIVE OR | ^ |
Truth Table for NOT
Truth Table for AND
A | B | A && B |
F | F | F |
F | T | F |
T | F | F |
T | T | T |
Truth Table for OR
A | B | A || B |
F | F | F |
F | T | T |
T | F | T |
T | T | T |
Truth Table for EXCLUSIVE OR
A | B | A ^ B |
F | F | F |
F | T | T |
T | F | T |
T | T | F |
Conditional Boolean Operator
If you have an expression of the form A && B then Java
evaluates A first. If A is true, then Java
evaluates B. If A is false, then Java does not
evaluate B.
If you have an expression of the form A || B then Java
evaluates A first. If A is true, then Java does
not evaluate B. If A is false, then Java does
evaluate B.
Bitwise Operators
The bitwise operators include the AND operator &, the OR
operator |, the EXCLUSIVE OR operator ^. The bitwise
operators applies to boolean and integer types. Both operands are always
evaluated.
Shift Operators
Shift operators are applied only to integer types.
- x << k : shift the bits in x, k places to the left
- x >> k : shift the bits in x, k places to the right filling in
with the highest bit on the left hand side
- x >>> k : shift the bits in x, k places to the right and fill in
with 0
Conditional Operator
The conditional operator ? : uses the boolean value of one
expression to decide which of the two other expressions should be
evaluated.
exp1 ? exp2 : exp3
If exp1 is true, then exp2 is chosen. If exp1 is false then exp3 is
chosen.
Operator Precedence
In Appendix C of your book you have a chart showing the list of
operators in Java and their precedence. When two operators have
the same precedence, their associativity determines the order
of evaluation. All binary operators are left associative. The
assignment operators are right associative.
Operand Evaluation Order
The left hand operand of a binary operator is evaluated before
any part of the right hand operand is evaluated. The order for
evaluating operands takes precedence over the operator precedence
rule.