Operators and Expressions

Operators

Java provides a rich operator environment. Most of its operators can be divided into the following four groups: arithmetic, bitwise, relational, and logical. 

Arithmetic Operators

Operator Result 
+ Addition (also unary plus) 
 Subtraction (also unary minus) 
* Multiplication 
/ Division 
% Modulus 
++ Increment 
– – Decrement 
+= Addition assignment 
– = Subtraction assignment 
*= Multiplication assignment 
/= Division assignment 
%= Modulus assignment 

Bitwise Operators

Operator Result 
~  Bitwise unary NOT 
Bitwise AND 
|  Bitwise OR 
Bitwise exclusive OR 
>> Shift right 
>>> Shift right zero fill 
<< Shift left 
&= Bitwise AND assignment 
|= Bitwise OR assignment 
^= Bitwise exclusive OR assignment 
>>= Shift right assignment 
>>>= Shift right zero fill assignment 

Relational Operators

Operator Result 
== Equal to 
!= Not equal to 
Greater than 
Less than 
>= Greater than or equal to 
<= Less than or equal to 

Boolean Logical Operators

Operator Result 
Logical AND 
Logical OR 
Logical XOR (exclusive OR) 
|| Short-circuit OR 
&& Short-circuit AND 
Logical unary NOT 
&= AND assignment 
|= OR assignment 
^= XOR assignment 
== Equal to 
!= Not equal to 
?: Ternary if-then-else 
A B A | B A & B A ^ B !A 
False False False False False True 
True False True False True False 
False True True False True True 
True True True True False False 

The Assignment Operator

  • The assignment operator is the single equal sign =
  • The assignment operator works the same in Java as much as it does in any other programming language.
  • It has this general form;

var = expression;

Operator Precedence

Operators Precedence 
postfix expr++ expr— 
unary ++expr –expr +expr -expr ~ ! 
multiplicative * / % 
additive + – 
shift << >> >>> 
relational < > <= >= instanceof 
equality == != 
bitwise AND 
bitwise exclusive OR 
bitwise inclusive OR 
logical AND && 
logical OR || 
ternary ? : 
assignment = += -= *= /= %= &= ^= |= <<= >>= >>>= 

Using Parentheses

Parentheses raise the precedence of the operations that are inside them. This is often necessary to obtain the result you desire. For example, consider the following expression:;

a + b * 3

This expression will first multiply b with 3, then add it to a. This is because multiply has higher precedence than addition. This expression can be rewritten as below;

a + (b * 3)

However, if you want the addition to takes place first, you need to put it within a parentheses (i.e. brackets). This then will yield a different result.

(a + b) * 3

Expressions

An expression is a construct made up of variables, operators, and method invocations, which are constructed according to the syntax of the language, that evaluates to a single value. 

		int index = 0;
		int[] numbers = new int[2];

		numbers[0] = 100;

		System.out.println("Element 1 at index 0: " + numbers[0]);

		int result = numbers[0];

		// check if index is equal to result
		if (index == result) {
			System.out.println("equal");
		} else {
			System.out.println("not equal");
		}