Skip to main content
Java Basics
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

OperatorNameExampleResult
+Addition10 + 313
-Subtraction10 - 37
*Multiplication10 * 330
/Division10 / 33 (integer division!)
%Modulus (remainder)10 % 31
java
123456
int a = 17, b = 5;
System.out.println("Addition: " + (a + b));       // 22
System.out.println("Subtraction: " + (a - b));    // 12
System.out.println("Multiplication: " + (a * b)); // 85
System.out.println("Division: " + (a / b));       // 3 (not 3.4!)
System.out.println("Modulus: " + (a % b));        // 2

Important: Integer division truncates the decimal! Use double for accurate division:

java
1
double result = 17.0 / 5;  // 3.4

4. Increment and Decrement Operators

OperatorNameExample
++Increment by 1i++ or ++i
--Decrement by 1i-- or --i
java
123456
int x = 5;
System.out.println(x++); // Prints 5 (post-increment: use THEN increment)
System.out.println(x);   // Prints 6

int y = 5;
System.out.println(++y); // Prints 6 (pre-increment: increment THEN use)

5. Relational (Comparison) Operators

OperatorMeaningExampleResult
==Equal to5 == 5true
!=Not equal to5 != 3true
>Greater than5 > 3true
<Less than5 < 3false
>=Greater than or equal5 >= 5true
<=Less than or equal5 <= 3false
java
123
int age = 20;
System.out.println(age >= 18);  // true (can vote)
System.out.println(age == 21);  // false

6. Logical Operators

OperatorMeaningExample
&&Logical ANDBoth must be true
||Logical ORAt least one must be true
!Logical NOTReverses the boolean
java
123456789101112131415161718
int age = 25;
boolean hasLicense = true;

// AND: both conditions must be true
if (age >= 18 && hasLicense) {
    System.out.println("You can drive!");
}

// OR: at least one must be true
if (age < 13 || age > 65) {
    System.out.println("Discount ticket!");
}

// NOT: reverses the result
boolean isRaining = false;
if (!isRaining) {
    System.out.println("Go for a walk!");
}

7. Assignment Operators

OperatorExampleEquivalent
=x = 5Assign 5 to x
+=x += 3x = x + 3
-=x -= 3x = x - 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3
%=x %= 3x = x % 3
java
1234
int score = 100;
score += 10;  // score is now 110
score -= 5;   // score is now 105
score *= 2;   // score is now 210

8. Ternary Operator

A shorthand for if-else:

Syntax: condition ? valueIfTrue : valueIfFalse

java
1234567
int age = 20;
String status = (age >= 18) ? "Adult" : "Minor";
System.out.println(status); // "Adult"

int a = 10, b = 20;
int max = (a > b) ? a : b;
System.out.println("Max: " + max); // 20

9. Bitwise Operators (Advanced Beginner)

OperatorNameDescription
&ANDBoth bits must be 1
|ORAt least one bit must be 1
^XORExactly one bit must be 1
~NOTFlips all bits
<<Left shiftShifts bits left
>>Right shiftShifts bits right
java
123456
int a = 5;  // Binary: 0101
int b = 3;  // Binary: 0011

System.out.println(a & b);  // 1  (0001)
System.out.println(a | b);  // 7  (0111)
System.out.println(a ^ b);  // 6  (0110)

10. Operator Precedence

Java evaluates operators in a specific order (highest to lowest):
  1. 1. () — Parentheses
  1. 2. ++, -- — Increment/Decrement
  1. 3. *, /, % — Multiplication, Division, Modulus
  1. 4. +, - — Addition, Subtraction
  1. 5. <, >, <=, >= — Relational
  1. 6. ==, != — Equality
  1. 7. && — Logical AND
  1. 8. || — Logical OR
  1. 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 / 2 returns 2, not 2.5.
  • Post vs Pre increment confusion: x++ and ++x behave 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. 1. Write a program that calculates the area and perimeter of a rectangle.
  1. 2. Write a program that swaps two numbers without using a third variable (hint: use XOR).
  1. 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 ++i and i++?
  • 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 the Scanner class to accept keyboard input from users.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·