Month: January 2012

As in mathematics, operators in Java are used to produce new values from operands. Choosing the right operator at the right time is important in order to write clear code in a fast way.

Code samples in this post were compiled with a JDK 6.

Assignment Operators

The most common assignment operator in Java is the = operator. This operator simply assign values to variables. Nevertheless, the = operator can be composed with other operators like +, -, *, /, >>, <<, & and | forming the compound operators: +=, -=, *=, /=, >>=, <<=, &= and |=.

int i = 0;
int j = 0;
i += 2; // Which is the same that i = i + 2;
j *= 10 + 4; // Which is the same that j = j * (10 + 4)

Relational Operators

Relational operators are often used on if tests and result in a boolean value (true or false). Java has six relational operators which are: >, >=, <, <=, ==, and !=. All of them can perform comparisons on integers, floating-points or characters. Only the last two (== and !=) are used to perform comaprisons on boolean values, or object reference variables.

instanceof Operator

The instanceof operator verifies if an object is of a particular type. To evaluate the type of an object (on the left), the operator uses a IS-A test for a class or interface (on the right). The IS-A test returns true if the object inherits from the class or implements the interface used in the test. Otherwise, the result is false.

Arithmetic Operators

Beyond the basic arithmetic operators +, -, *, and /, there are three other operators also widely used which are:

the Remainder operator (%);

the String Concatenation operator (+);

the Increment and Decrement operators (++ and --, respectively).

The remainder operator % returns the remainder of a division operation.

int x = 10;
int y = 4;
int rem = x % y; // which will be 2

Observation: in Java expressions are evaluated from left to right. The operators *, /, and % have higher priority then + and - operators. Parentheses can be used to change the evaluation order.

The operator + can act as addition operator or string concatenation operator, which depends on the operands. If there is a string among the operands, the + becomes a string concatenation operator. If the operands are numbers, the + will perform the addition.

Finally, the increment and decrement operators will increment or decrement variables by one. The operator can prefix or postfix the operand. When the prefix approach is used, the operator is placed before the variable, and the operation is performed before the variable is used. When the postfix approach is employed, the operator is placed after the variable, and the increment or decrement operation takes place after the variable is used.

Short-Circuit Logical Operators

A short-circuit AND (&&) will evaluate the left side of an expression first. If the result is false, the operator will not waste time evaluating the right side and will return false. The right side will be evaluated only if the result of the left side is true. If the result of both sides is true, the hole expression is evaluated as true. If one of them results false, the expression is evaluated as false.

As the short-circuit AND, the short-circuit OR (||) will start evaluating the left side of the expression. Nevertheless, if the result is true the right side will not be analysed and the result of the whole expression will be true. The right side will be evaluated only if the result of the expression on left side is false. So, if there is one true, the global result is true. If both sides are false, the final result is false.

Not Short-Circuit Logical Operators

Not short-circuit operators are represented as follows:

| non-short-circuit OR

& non-short-circuit AND

As not short-circuit operators will evaluate logical expressions like the operators && and ||. On the other hand, not short-circuit operators will evaluated ALWAYS both sides of the expression. Non-short-circuit OR (|) will return false if both sides of the logical expression are false. It returns true if one side is true. Non-short-circuit AND (&) returns true only if both sides are evaluated as true. Otherwise, it returns false.

Bitwise Operators

Bitwise operators are operators that evaluate the bit value of each operand. They are used in expressions with integer values. If the operand is smaller than int, the primitive value will be converted to an int.

Following a list with the bitwise operators in Java.

~ unary bitwise complement

& bitwise AND

| bitwise OR

^ bitwise XOR

Shift operators: <<, >>, and >>>

The unary bitwise complement simply invert the bit value. The bitwise operators AND, OR and XOR evaluate bits using simple logic. The shift operator << shifts bits of to left putting zeros on the right side (low order position). The shift operator >> shifts bits to the right side saving the number sign. In other words, a negative number remains negative after the operation. Finally, the shift operator >>> move bits to right without saving the number sign. In this case, zeros are inserted as most significant bits (on the left). All the operators can be also used with the assignment (=) operator.

Bitwise operators are generally used to pack a lot of information into a single variable, like masks, flags, etc.