Limited Offer
60% OFF on all plans!

Operators in Java

Master arithmetic, relational, logical, bitwise, assignment, and ternary operators, plus operator precedence rules.

Arithmetic Operators

Arithmetic operators work on numeric types. Integer division truncates the decimal part. The modulo operator `%` gives the remainder and is essential for even/odd checks, cycling, and clock arithmetic.

1. Basic Arithmetic (+, -, *, /, %)

The five core arithmetic operators and their behaviour with integers and doubles.

2. Increment & Decrement (++, --)

Prefix form changes the value before use; postfix form changes it after use.

3. Integer vs Float Division Gotcha

A common beginner mistake: both operands must be floating-point to get a decimal result.

Relational (Comparison) Operators

Relational operators compare two values and always return `true` or `false`. They are the foundation of every `if`, `while`, and `for` condition.

1. ==, !=, <, >, <=, >=

The six comparison operators work on all numeric types and `char`. Use `.equals()` for objects.

2. instanceof Operator

Check whether an object is an instance of a class. Java 16+ adds pattern matching to combine the check and cast.

Logical Operators

Logical operators combine two or more boolean expressions. Java uses short-circuit evaluation — `&&` stops as soon as it finds `false`, and `||` stops as soon as it finds `true`. This is important for null safety and performance.

1. && (AND), || (OR), ! (NOT)

The three core logical operators and their truth table behaviour.

2. Short-Circuit Evaluation

Java stops evaluating as soon as the result is determined — use this to prevent NullPointerException.

Assignment Operators

Assignment operators write a value into a variable. Compound assignment operators (`+=`, `-=`, `*=`, `/=`, `%=`) combine an arithmetic operation with assignment, making code shorter and clearer.

1. Compound Assignment (+=, -=, *=, /=, %=)

`a += b` is shorthand for `a = a + b`. All five arithmetic operators have a compound form.

2. Chained Assignment

Assign the same value to multiple variables in a single statement.

Bitwise Operators

Bitwise operators work directly on the binary representation of integers. They are used in low-level programming, flags, permissions, encryption, and performance-critical code.

1. &, |, ^, ~ (AND, OR, XOR, NOT)

Apply logical operations bit by bit across two integers.

2. Shift Operators (<<, >>, >>>)

Shift bits left or right. Left shift multiplies by powers of 2; right shift divides.

Operator Precedence

Java evaluates operators in a defined order — higher precedence operators are applied before lower ones. When in doubt, use parentheses `()` to make the intended order explicit.

1. Precedence in Practice

See how precedence affects results and how parentheses override the default order.

2. Operator Precedence Table

Full Java operator precedence from highest (1) to lowest. Higher level operators are evaluated first.