Previous | Table of Contents | Next |
Java supports almost all of the standard C operators. These standard operators have the same precedence and associativity in Java as they do in C. They are listed in Table 14.3 .
Prec. | Operator | Operand Types | Assoc. | Operation Performed |
---|---|---|---|---|
1 | ++ | Arithmetic | R | Pre-or-post increment (unary) |
-- | Arithmetic | R | Pre-or-post decrement (unary) | |
+, - | Arithmetic | R | Unary plus, unary minus | |
- | Integral | R | Bitwise complement (unary) | |
! | boolean | R | Logical complement (unary) | |
(type) | Any | R | Case | |
2 | *, /, % | Arithmetic | L | Multiplication, division, remainder |
3 | +, - | Arithmetic | L | Addition, subtraction |
+ | String | L | String concatenation | |
4 | << | Integral | L | Left shift |
>> | Integral | L | Right shift with sign extension | |
>>> | Integral | L | Right shift with zero extension | |
5 | <, <= | Arithmetic | L | Less than, less than or equal |
>, >= | Arithmetic | L | Greater than, greater than or equal | |
instanceof | Object, type | L | Type comparison | |
6 | == | Primitive | L | Equal (have identical values) |
!= | Primitive | L | Note equal (have difference values) | |
== | Object | L | Equal (refer to same object) | |
!= | Object | L | Not equal (refer to difference objects) | |
7 | & | Integral | L | Bitwise AND |
& | boolean | L | boolean AND | |
8 | ^ | Integral | L | Bitwise XOR |
^ | boolean | L | boolean XOR | |
9 | | | Integral | L | Bitwise OR |
| | boolean | L | boolean OR | |
10 | && | boolean | L | boolean OR |
11 | || | boolean | L | Conditional AND |
12 | ?: | boolean, any | R | Conditional (ternary) operator |
13 | = | Variable, any | R | Assignment |
*=, /=, %=, +=, -=, <<=, >>=, >>>=, &=, ^=, |= | variable, any | R | assignment with operators | |
Note the following Java operator differences from C. Java does not support the comma operator for combining two expressions into one (although the for statement simulates this operator in a useful way). Since Java does not allow you to manipulate pointers directly, it does not support the reference and dereference operators *, ->, and &, nor the sizeof operator. Further, Java doesnt consider [] (array access) and . (field access) to be operators, as C does.
Java also adds some new operators:
8To C++ programmers, this looks like operator overloading. In fact, Java does not support operator overloadingthe language designers decided (after much debate) that overloaded operators were a neat idea, but that code that relied on them became hard to read and understand.
Previous | Table of Contents | Next |