CHAPTER 05
Beginner
Operators in Java
Updated: May 17, 2026
5 min read
# CHAPTER 5
Operators in Java
1. Introduction
Operators are special symbols that perform operations on variables and values. They are the verbs of programming — they *do* things. Just as math uses+, -, ×, and ÷, Java provides a rich set of operators for calculations, comparisons, and logic.
2. Learning Objectives
- Use arithmetic operators for calculations.
- Compare values with relational operators.
- Combine conditions with logical operators.
- Understand assignment and compound assignment operators.
- Use bitwise and ternary operators.
3. Arithmetic Operators
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 10 + 3 | 13 |
- | Subtraction | 10 - 3 | 7 |
* | Multiplication | 10 * 3 | 30 |
/ | Division | 10 / 3 | 3 (integer division!) |
% | Modulus (remainder) | 10 % 3 | 1 |
java
Important: Integer division truncates the decimal! Use double for accurate division:
java
4. Increment and Decrement Operators
| Operator | Name | Example |
|---|---|---|
++ | Increment by 1 | i++ or ++i |
-- | Decrement by 1 | i-- or --i |
java
5. Relational (Comparison) Operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 5 | true |
!= | Not equal to | 5 != 3 | true |
> | Greater than | 5 > 3 | true |
< | Less than | 5 < 3 | false |
>= | Greater than or equal | 5 >= 5 | true |
<= | Less than or equal | 5 <= 3 | false |
java
6. Logical Operators
| Operator | Meaning | Example |
|---|---|---|
&& | Logical AND | Both must be true |
|| | Logical OR | At least one must be true |
! | Logical NOT | Reverses the boolean |
java
7. Assignment Operators
| Operator | Example | Equivalent |
|---|---|---|
= | x = 5 | Assign 5 to x |
+= | x += 3 | x = x + 3 |
-= | x -= 3 | x = x - 3 |
*= | x *= 3 | x = x * 3 |
/= | x /= 3 | x = x / 3 |
%= | x %= 3 | x = x % 3 |
java
8. Ternary Operator
A shorthand forif-else:
Syntax: condition ? valueIfTrue : valueIfFalse
java
9. Bitwise Operators (Advanced Beginner)
| Operator | Name | Description |
|---|---|---|
& | AND | Both bits must be 1 |
| | OR | At least one bit must be 1 |
^ | XOR | Exactly one bit must be 1 |
~ | NOT | Flips all bits |
<< | Left shift | Shifts bits left |
>> | Right shift | Shifts bits right |
java
10. Operator Precedence
Java evaluates operators in a specific order (highest to lowest):-
1.
()— Parentheses
-
2.
++,--— Increment/Decrement
-
3.
*,/,%— Multiplication, Division, Modulus
-
4.
+,-— Addition, Subtraction
-
5.
<,>,<=,>=— Relational
-
6.
==,!=— Equality
-
7.
&&— Logical AND
-
8.
||— Logical OR
-
9.
=,+=,-=— Assignment
Tip: When in doubt, use parentheses to make your intent clear!
11. Common Mistakes
-
Using
=instead of==:if (x = 5)is assignment, not comparison.
-
Integer division surprise:
5 / 2returns2, not2.5.
-
Post vs Pre increment confusion:
x++and++xbehave differently when used in expressions.
12. Best Practices
- Always use parentheses for complex expressions to improve readability.
-
Use compound operators (
+=,-=) for cleaner code.
- Use the ternary operator for simple conditional assignments only.
13. Exercises
- 1. Write a program that calculates the area and perimeter of a rectangle.
- 2. Write a program that swaps two numbers without using a third variable (hint: use XOR).
- 3. Write a program using the ternary operator to check if a number is even or odd.
14. MCQ Quiz with Answers
Question 1
What is the result of 10 / 3 in Java (both ints)?
Question 2
What does the % operator return?
Question 3
What is the result of !(true)?
Question 4
What does x += 5 mean?
Question 5
What is (5 > 3) ? "Yes" : "No"?
Question 6
What is 5 == 5?
Question 7
What prints: int x = 5; System.out.println(x++);?
Question 8
Which operator has the highest precedence?
Question 9
What is true && false?
Question 10
What is true || false?
15. Interview Questions
-
Q: What is the difference between
++iandi++?
-
Q: What is short-circuit evaluation in
&&and||?
- Q: How does integer division work in Java?
- Q: Can the ternary operator be nested? Should it be?
16. Summary
Java provides arithmetic, relational, logical, assignment, bitwise, and ternary operators. Understanding operator precedence prevents bugs. The ternary operator is a concise alternative to simple if-else blocks. Always be careful with integer division and increment operators.17. Next Chapter Recommendation
In Chapter 6: User Input and Output, we'll learn how to make programs interactive using theScanner class to accept keyboard input from users.