Operators
Operators let you calculate, compare and combine values.
On this page · 5 min · Beginner
On this page
- In simple words
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 30, 2026
Related tutorials
In simple words#
Operators are calculator buttons: + − × ÷ for maths, comparison buttons that answer yes/no, and logic buttons (and, or, not) that combine yes/no answers.
- %
- Remainder after division.
- && / ||
- And / or, with short-circuit evaluation.
- ?:
- Ternary: condition ? valueIfTrue : valueIfFalse.
Learning objectives#
- Use arithmetic operators
- Understand integer division
- Combine conditions with logical operators
- Use the ternary operator
Explanation#
Arithmetic: + - * / %. Dividing two ints gives an int (7 / 2 = 3).
Comparison: == != > < >= <= produce booleans. Compare objects (like Strings) with equals(), not ==.
Logical: && (and), || (or), ! (not). && and || short-circuit — the right side runs only if needed.
Assignment shortcuts (+=, -=), increment (++, --) and the ternary operator condition ? a : b.
Code example: Operators in a discount calculator#
int total = 2499;
int items = 4;
System.out.println(total / items); // 624 (integer division)
System.out.println(total % items); // 3 (remainder)
System.out.println((double) total / items); // 624.75
boolean isMember = true;
boolean bigOrder = total >= 2000;
boolean freeDelivery = isMember || bigOrder;
String label = freeDelivery ? "Free delivery" : "₹49 delivery";
int points = 10;
points += 5; // 15
points++; // 16
System.out.println(label + ", points = " + points);
624 3 624.75 Free delivery, points = 16
This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- int / int drops the decimals.
- Casting one operand to double keeps them.
- || is true if either side is true.
- The ternary picks a label in one line.
Real-world use case#
Shopping carts use these operators for totals, discounts, free-delivery rules and loyalty points.
Common mistakes#
5 / 2 is 2. Make one operand a double: 5 / 2.0.
= assigns; == compares.
Best practices#
- Use parentheses for clarity
- Cast before dividing when decimals matter
- Keep ternaries short
- Compare strings with equals()
Remember this#
int / int = int.
% gives the remainder.
&& and || short-circuit.
Practice exercise#
Your task: Check whether a number n is even using %.
Show solution
boolean isEven = n % 2 == 0;
Mini quiz#
What is 17 % 5?
Want more? Practise all Java MCQs · Java interview questions
My notes#
Saved in this browser.