Operators.java

Java Operators

A working reference for the symbols that make Java code compute — with a closer look at bit-shifting and the ternary shortcut, worked out like they'd run in a console.

01

Arithmetic Operators

Standard math on numeric values.

OpMeaningExampleResult
+Addition5 + 38
-Subtraction5 - 32
*Multiplication5 * 315
/Division5 / 22 (int)
%Modulus5 % 21
02

Assignment Operators

Store or update a value in a variable.

OpMeaningEquivalent to
=Assignx = 5
+=Add & assignx = x + 3
-=Subtract & assignx = x - 3
*=Multiply & assignx = x * 3
/=Divide & assignx = x / 3
%=Modulus & assignx = x % 3
03

Relational Operators

Compare two values; always evaluate to a boolean.

OpMeaning
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
Use .equals() to compare object content (like Strings) — == on objects compares references, not values.
04

Logical Operators

Combine boolean expressions.

OpMeaning
&&Logical AND (short-circuits)
||Logical OR (short-circuits)
!Logical NOT
05

Unary Operators

Act on a single operand.

OpMeaning
+ / -Unary plus / minus
++Increment (pre or post)
--Decrement (pre or post)
!Logical NOT
06

Bitwise Operators

Operate directly on the binary representation of integers.

OpMeaning
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise complement
<<Left shift — covered below
>>Right shift — covered below
>>>Unsigned right shift
07

Left Shift Operator <<

Shifts every bit to the left by n places, filling the vacated positions on the right with zero. Each left shift by one place doubles the value — it's the bitwise equivalent of multiplying by 2ⁿ.

5 (before)
0
0
0
0
0
1
0
1
5 << 2
0
1
0
1
0
0
·
·
bits move two places left → 5 becomes 20 (5 × 2²)
LeftShiftDemo.javaJDK 17
1 2 3 4 5 6 7 8 9 10
public class LeftShiftDemo { public static void main(String[] args) { int num = 5; // binary: 0000 0101 int shiftedBy1 = num << 1; // 5 * 2^1 = 10 int shiftedBy2 = num << 2; // 5 * 2^2 = 20 System.out.println("5 << 1 = " + shiftedBy1); System.out.println("5 << 2 = " + shiftedBy2); } }
Console output $ 5 << 1 = 10
$ 5 << 2 = 20
08

Right Shift Operator >>

Shifts every bit to the right by n places, refilling from the left with the sign bit (0 for positive numbers, 1 for negative). It's the bitwise equivalent of dividing by 2ⁿ, rounding toward negative infinity.

20 (before)
0
0
0
1
0
1
0
0
20 >> 2
0
0
0
0
0
1
0
1
bits move two places right, sign bit (0) fills in → 20 becomes 5 (20 ÷ 2²)
RightShiftDemo.javaJDK 17
1 2 3 4 5 6 7 8 9 10
public class RightShiftDemo { public static void main(String[] args) { int num = 20; // binary: 0001 0100 int shiftedBy1 = num >> 1; // 20 / 2^1 = 10 int shiftedBy2 = num >> 2; // 20 / 2^2 = 5 System.out.println("20 >> 1 = " + shiftedBy1); System.out.println("20 >> 2 = " + shiftedBy2); } }
Console output $ 20 >> 1 = 10
$ 20 >> 2 = 5
Note: >> is an arithmetic shift and preserves the sign of negative numbers. If you need the vacated bits filled with 0 regardless of sign, use the unsigned right shift >>> instead.
09

Ternary Operator ?:

A compact stand-in for an if-else statement that returns a value in a single expression: condition ? valueIfTrue : valueIfFalse.

if condition is true age >= 18 → "Adult"
? :
if condition is false age < 18 → "Minor"
TernaryDemo.javaJDK 17
1 2 3 4 5 6 7 8 9 10 11 12
public class TernaryDemo { public static void main(String[] args) { int age = 20; // condition ? valueIfTrue : valueIfFalse String category = (age >= 18) ? "Adult" : "Minor"; System.out.println("Category: " + category); // ternary can also be nested int marks = 76; String grade = (marks >= 90) ? "A" : (marks >= 75) ? "B" : "C"; System.out.println("Grade: " + grade); } }
Console output $ Category: Adult
$ Grade: B
Tip: Nesting ternaries (as with grade above) works, but keep it to one or two levels — beyond that, an if-else chain reads far more clearly.
10

Operator Precedence

When an expression mixes several operators, Java doesn't evaluate left to right — it follows a fixed pecking order. Higher rows bind tighter and run first; operators on the same row run according to their associativity.

RankOperatorsCategoryAssociativity
1expr++  expr--PostfixLeft → Right
2++expr  --expr  +expr  -expr  !  ~UnaryRight → Left
3*  /  %MultiplicativeLeft → Right
4+  -AdditiveLeft → Right
5<<  >>  >>>ShiftLeft → Right
6<  >  <=  >=RelationalLeft → Right
7==  !=EqualityLeft → Right
8&Bitwise ANDLeft → Right
9^Bitwise XORLeft → Right
10|Bitwise ORLeft → Right
11&&Logical ANDLeft → Right
12||Logical ORLeft → Right
13?  :TernaryRight → Left
14=  +=  -=  *=  /=  %=  &=  ^=  |=  <<=  >>=  >>>=AssignmentRight → Left
rank 1 = highest Rows are ordered from tightest-binding (evaluated first) to loosest-binding (evaluated last). Use parentheses to override the default order whenever it makes an expression clearer.
PrecedenceDemo.javaJDK 17
1 2 3 4 5 6 7 8 9 10 11 12
<
Tip: Assignment sits at the very bottom of the table, which is why x = y = 5 works — the rightmost assignment happens first, then its result feeds the next one going left.