Java Programming Lesson 3 | Operators in Java Tutorial
LESSON-3: OPERATORS
Operators are one of the most important building blocks of Java programming. They allow you to perform calculations, compare values, assign data, and make decisions inside your program.
In this lesson, you will learn:
- What operators are
- Types of operators in Java
- How each operator works with examples
- Common mistakes beginners make
- Real-world practice examples
Let’s start!
🔷 What Are Operators in Java?
int a = 10 + 5; // '+' is an operator🔷 Types of Operators in Java
| Operator | Meaning | Example |
|---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus (remainder) | a % b |
int a = 10, b = 3;
System.out.println(a + b); System.out.println(a + b); // 13System.out.println(a - b); // 7System.out.println(a * b); // 30System.out.println(a / b); // 3System.out.println(a % b); // 1⭐ Assignment Operators — Assign Values
| Operator | Meaning | Example |
|---|---|---|
= | Assign | a = b |
+= | a = a + b | a += b |
-= | a = a - b | a -= b |
*= | a = a * b | a *= b |
/= | a = a / b | a /= b |
%= | a = a % b | a %= b |
int x = 10;x += 5; // x = 15x *= 2; // x = 30⭐ Unary Operators — One-Operand Operations
| Operator | Meaning | Example |
|---|---|---|
+ | Positive | +a |
- | Negative | -a |
++ | Increment | a++ or ++a |
-- | Decrement | a-- or --a |
! | Logical NOT | !true |
int a = 5;System.out.println(++a); // 6System.out.println(a--); // 6 (then becomes 5)⭐ Relational (Comparison) Operators — Compare Values
| Operator | Meaning | Example |
|---|---|---|
== | Equal to | a == b |
!= | Not equal | a != b |
> | Greater than | a > b |
< | Less than | a < b |
>= | Greater or equal | a >= b |
<= | Less or equal | a <= b |
int a = 10, b = 20;System.out.println(a < b); // true⭐ Logical Operators — Combine Conditions
| Operator | Meaning |
|---|---|
&& | Logical AND |
| ` | |
! | Logical NOT |
int age = 20;boolean hasID = true;
if (age >= 18 && hasID) { System.out.println("Eligible");}⭐ Bitwise Operators — Work on Binary Values
| Operator | Meaning |
|---|---|
& | AND |
| ` | ` |
^ | XOR |
<< | Left Shift |
>> | Right Shift |
System.out.println(5 & 3); // 1System.out.println(5 | 3); // 7⭐ Ternary Operator — Shortcut for if-else
Syntax:
condition ? value1 : value2;int marks = 75;String result = (marks >= 40) ? "Pass" : "Fail";⭐ Increment / Decrement Operators
| Operator | Meaning |
|---|---|
++ | Increase by 1 |
-- | Decrease by 1 |
int n = 10;
System.out.println(n++); System.out.println(n++); // 10 (then becomes 11)System.out.println(++n); // 12🔷 Real-World Examples Using Operators
int length = 5, width = 4;int area = length * width;int num = 7;System.out.println(num % 2 == 0 ? "Even" : "Odd");String user = "admin";String pass = "123";
if (user.equals("admin") && pass.equals("123")) { System.out.println("Login Successful");}🔷 Common Mistakes Beginners Make
❌ Using
== to compare stringsname.equals("Java")% gives remainder, not percentage.❌ Wrong use of increment operator
System.out.println(n++); // prints old valueSystem.out.println(++n); // prints updated value5 / 2 = 2 // not 2.5🔷 Summary of What You Learned
In this lesson, you learned all important operator types in Java:
- Arithmetic
- Assignment
- Unary
- Relational
- Logical
- Bitwise
- Ternary
- Increment & Decrement
Operators help your Java program perform calculations, compare data, make decisions, and control the flow of execution.
✅ Full Practical Example
✅ Output:
🧠 Bonus: Increment / Decrement Operator
| Operator | Meaning |
|---|---|
| ++ | Add 1 |
| -- | Subtract 1 |
📝 Exercise for You (Try Now ✅)
Write a program:
Input:
Output must show:
✅ Sum
✅ Difference
✅ a > b ?
✅ a == b ?
✅ (a > 30 AND b < 25)
Reply with your code + result
I will check and grade it ✅
🎯 Quick Quiz
1️⃣ Which operator gives remainder?
A) /
B) %
C) *
2️⃣ Which operator returns TRUE only when both conditions are TRUE?
A) &&
B) ||
C) !
Reply like:
1 → ?, 2 → ?
✅ Lesson-3 Complete!
Reply with:
✅ Exercise code
✅ Quiz answers
➡ and then we go to Lesson-4: Conditional Statements (if, else, switch) 🚀
▶ NEXT PART - Lesson-4: Conditional Statements
Frequently Asked Questions (FAQs)
What are operators in Java?
Operators in Java are special symbols used to perform operations on variables and values. They help in mathematical calculations, comparisons, and logical processing.
What are the types of operators in Java?
Java provides several operator categories including arithmetic, relational, logical, assignment, unary, bitwise, and ternary operators.
What are arithmetic operators in Java?
Arithmetic operators such as +, -, *, /, and % are used to perform basic mathematical operations like addition, subtraction, multiplication, division, and modulus.
What are relational operators in Java?
Relational operators like ==, !=, >, <, >=, and <= compare values and return true or false.
What are logical operators used for?
Logical operators (&&, ||, !) are used to combine multiple conditions and provide results based on boolean logic.
What is the assignment operator in Java?
The assignment operator (=) assigns values to variables. Java also provides compound assignment operators such as +=, -=, *=, and /=.
What are unary operators?
Unary operators work with a single operand. Examples include increment (++), decrement (--), unary plus, unary minus, and logical NOT.
What is the ternary operator in Java?
The ternary operator (?:) is a shorthand for an if-else statement. It evaluates a condition and returns one of two values.
What are bitwise operators?
Bitwise operators perform operations on individual bits of integer values. Examples include &, |, ^, <<, and >>.
Why are operators important in Java programming?
Operators make calculations, comparisons, assignments, and condition evaluations easier, enabling developers to build efficient and logical programs.

No comments
Thank You For Visit My Website.
I Will Contact As Soon As Possible.