Sunday 13 March 2016

The Java Language: Operators

Java supports almost all standard operators from the C language. These operators also have the same precedence in Java as they do in C, as shown in the following Table.

Precedence Operator Operand Type Description
1 ++,-- Arithmetic Increment and Decrement
1 +,- Arithmetic Unary Plus and unary Minus
1 ~ Integral Bitwise complement
1 ! Boolean Logical Complement
1 (type) Any Cast
2 *,/,% Arithmetic Multiplication, division, remainder
3 +,- Arithmetic Addition and subtraction
3 + String String concatenation
4 << Integral Left Shift
4 >> Integral Right shift with sign extension
4 >>> Integral Right shift with no extension
5 <,<=,>,>= Arithmetic Numeric Comparison
5 instanceof Object Type comparison
6 ==,!= Primitive Equality and inequality of value
6 ==, != Object Equality and inequality of reference
7 & Integral Bitwise AND
7 & Boolean Boolean AND
8 ^ Integral Bitwise XOR
8 ^ Boolean Boolean XOR
9 | Integral Bitwise OR
9 | Boolean Boolean OR
10 && Boolean Conditional AND
11 || Boolean Conditional OR
12 ?: N/A Conditional ternary operator
13 = Any Assignment
We should also note that the percent (%) operator is not strictly a modulo, but a remainder, and can have a negative value. Java also adds some new operators. As we’ve seen, the + operator can be used with String values to perform string concatenation. Because all integral types in Java are signed values, the >> operator can be used to perform a right-arithmetic-shift operation with sign extension. The >>> operator treats the operand as an unsigned number and performs a right-arithmetic-shift with no sign extension. The new operator is used to create objects; we will discuss it in detail shortly.

0 comments:

Post a Comment