Java Operators

Now that we know about variables we will want to increment them, add them, shift there bits and compare one to another. There are a number of operators in Java that are very similar to other programming languages, there can be divide into categories

Arithmetic Operators

Arithmetic operators are used in mathematical expressions in the same way they are used in algebra, operands of arithmetic must be either numeric or char (char is a subset of int).

Operator Result
+ Addition (also unary plus)
- Subtraction (also unary minus)
* Multiplication
/ Division
% Modulus
++ Incremental
-- Decremental
+= Additional assignment
-= Subtractional assignment
*= Multiplication assignment
/= Division assignment
%= Modulus assignment
Examples
int x = 5 * 5;
int y = 20 - 10;
int z = 20 % 3;

int z += 5;
int x -= 2;

int x = 5 * 5;
int y = 20 - 10;
int z = 20 % 3;

Bitwise Operators

Bitwise operators can be applied to integer types long, int, short, char and byte

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
<<= Shift left assignment
Examples

int a = 10, b = 9;
int c = a & b;             # results in 8 being assigned to c (1010 & 1001 = 1000)
int c = a | b;             # results in 11 being assigned to c (1010 | 1001 = 1011)

X Y X & Y (AND) X | Y (OR) X ^ Y (XOR) ~X (unary NOT)
0 0 0 0 0 1
0 1 0 1 1 0
1 0 0 1 1 1
1 1 1 1 0 0
int x = 5;
int y = ~x;
System.out.println("Y: " + y);     # results in "Y: -6"

~0000 0000 0000 0000 0000 0000 0000 0101 becomes 1111 1111 1111 1111 1111 1111 1111 1010

int result = 95 >> 1;      # shift the bits to the right by one
int result = 95 << 2;      # shift the bits to the left by two
int result = 95 >>> 1;     # shift the bits to the right by 1 (fill the left most bits with zero's)

Relational Operators

Relational operators determine the relationship of one operand has to an other, they determine equality and ordering

Operator Result
== Equal to
!= Not Equal to
> Greater Than
< Less Than
>= greater Than or equal to
<= Less Than or equal to
Examples
if ( age > 65 ) { ... }           # greater than operator
if ( age < 65 ) { ... }           # less than operator
if ( age >= 65 ) { ... }          # greater than or equal to operator
if ( age <= 65 ) { ... }          # less than or equal to operator

Note: comparison operators always result in a boolean (true or false)

int age = 60;
if ( age == 60 ) { ... }          # compare variable age with integer 60
if ( age != 60 ) { ... }

String s1 = "Test";
String s2 = "Test";
if ( s1 == s2 ) { ... }           # compare two String objects

Logical Operators

Boolan logical operators are used only operate on boolean operands, they have two operands which result in a boolean value.

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
Examples

A B A|B A & B A ^ B !A
False False False False False False
True False True False true False
False True True False True True
True True True True False False
# Short-Circuit
if (( age >= 5 ) && ( age <= 17 )) { ... }       # both expressions have to be true to execute the if body
if (( age == 18 ) || ( age == 19 )) { ... }      # only one expression has to be true to execute the if body

# Not Short Circuit
if (( age >= 5 ) & ( age <= 17 )) { ... }        # both expressions have to be true to execute the if body
if (( age == 18 ) | ( age == 19 )) { ... }       # only one expression has to be true to execute the if body

Note: using the not short circuit (& or |) is inefficient because it will evaluate both expressions even if the second expression would not change the outcome.

# Tenary operator is a short-cut version of a if-then-else statement
String status = (numOfPets < 3) ? "It's OK to have a couple of pets" : "Way too many";
          

Operator Precedence Chart

All operators have a order of precedence, they can also operate from left-to-right or from right-to-left. The table below starts with the highest order of precedence the parenthese () and desends to the lowest order of precedence.

Operator Type
Associativity
() parentheses
left to right
[ ] array subscript
"
. member selection
"
++ unary postincrement
right to left
-- unary postdecrement
"
++ unary preincrement
"
-- unary predecrement
"
+ unary plus
"
- unary minus
"
! unary logical negation
"
~ unary bitwise complement
"
( type ) unary cast
"
* multiplication
left to right
/ division
"
% modulus (reminder)
"
+ addition
"
- substraction
"
<< bitwise left shift
"
>> bitwise right shift with sign extension
"
>>> bitwise right shift with zero extension
"
< relational less than
"
<= relational less than or equal to
"
> relational greater than
"
>= relational greater than or equal to
"
instanceof type comparison
"
== relational is equal to
"
!= relational is not equal to
"
& bitwise AND
"
^ bitwise exclusive OR, boolean logical exclusive OR
"
| bitwise inclusive OR, boolean logical inclusive OR
"
&& logical AND
"
|| logical OR
"
?: ternary conditional
right to left
= assignment
"
+= additional assignment
"
-= substraction assignment
"
*= multiplication assignment
"
/= division assignment
"
%= modulus assignment
"
&= bitwise AND assignment
"
^= bitwise exclusive OR assignment
"
|= bitwise inclusive OR assignment
"
<<= bitwise left shift assignment
"
>>= bitwise right shift with sign extension assignment
"
>>>= bitwise right shift with zero extension assignment
"