Back to course

Arithmetic, Relational, and Logical Operators

Java Mastery: From Zero to Professional Developer (50-Lesson Journey)

Lesson 8: Arithmetic, Relational, and Logical Operators

Operators are symbols that perform operations on variables and values.

1. Arithmetic Operators

Used for mathematical calculations.

OperatorDescriptionExample
+Addition5 + 3 = 8
-Subtraction5 - 3 = 2
*Multiplication5 * 3 = 15
/Division5 / 2 = 2 (Integer division) or 5.0 / 2.0 = 2.5
%Modulus (Remainder)5 % 2 = 1
++Increment (adds 1)i++
--Decrement (subtracts 1)i--

Increment/Decrement Caveat (Pre vs. Post)

java int x = 10; int y = ++x; // Pre-increment: x becomes 11, then y becomes 11

int a = 10; int b = a++; // Post-increment: b becomes 10, then a becomes 11

2. Relational (Comparison) Operators

These compare two values and return a boolean result (true or false).

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

3. Logical Operators

Used to combine two or more boolean expressions.

OperatorDescription
&&Logical AND (Both conditions must be true)
`
!Logical NOT (Inverts the boolean value)

java int age = 20; boolean hasLicense = true;

// Logical AND example boolean canDrive = (age >= 16) && hasLicense; // true

// Logical NOT example boolean isMinor = !(age >= 18); // false

Note on Short-Circuiting: Java uses short-circuit evaluation for && and ||. If the result of the expression can be determined by evaluating the first operand, the second operand is skipped. (e.g., if the first part of A && B is false, B is never checked).