DMCA.com Protection Status Java Programming Lesson 3 | Operators in Java Tutorial - ZAREEN TECH WAVE -Largest Tutorials Website For Freelancer ZAREEN TECH WAVE -Largest Tutorials Website For Freelancer

Header Ads

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


⭐ Arithmetic Operators — For Calculations

Operator      Meaning                            Example
+                        Addition                              a + b
-                       Subtraction                              a - b
*                      Multiplication                              a * b
/                      Division                              a / b
%                      Modulus (remainder)                              a % b

Example:

int a = 10, b = 3;
System.out.println(a + b);
System.out.println(a + b); // 13
System.out.println(a - b); // 7
System.out.println(a * b); // 30
System.out.println(a / b); // 3
System.out.println(a % b); // 1

⭐ Assignment Operators — Assign Values


OperatorMeaningExample
=Assigna = b
+=a = a + ba += b
-=a = a - ba -= b
*=a = a * ba *= b
/=a = a / ba /= b
%=a = a % ba %= b

Example:

int x = 10;
x += 5; // x = 15
x *= 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

Example:

int a = 5;
System.out.println(++a); // 6
System.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

Example:

int a = 10, b = 20;
System.out.println(a < b); // true

⭐ Logical Operators — Combine Conditions


Operator                                   Meaning
&&                                      Logical AND
`
!                                      Logical NOT

Example:

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

Example:

System.out.println(5 & 3); // 1
System.out.println(5 | 3); // 7

⭐ Ternary Operator — Shortcut for if-else


Syntax:

condition ? value1 : value2;

Example:

int marks = 75;
String result = (marks >= 40) ? "Pass" : "Fail";

⭐ Increment / Decrement Operators


Operator                                 Meaning
++                                   Increase by 1
--                                  Decrease by 1

Example:

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

✔ Example 1: Calculate Area

int length = 5, width = 4;
int area = length * width;

✔ Example 2: Odd or Even

int num = 7;
System.out.println(num % 2 == 0 ? "Even" : "Odd");

✔ Example 3: Login Validation

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 strings

name.equals("Java")

❌ Modulus confusion

% gives remainder, not percentage.

❌ Wrong use of increment operator

System.out.println(n++); // prints old value
System.out.println(++n); // prints updated value

❌ Division with integers

5 / 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

public class OperatorsExample { public static void main(String[] args) { int x = 15, y = 10; System.out.println("x + y = " + (x + y)); System.out.println("x > y: " + (x > y)); System.out.println("x == y: " + (x == y)); System.out.println("Logical AND (x>10 && y<20): " + (x > 10 && y < 20)); } }

✅ Output:

x + y = 25 x > y: true x == y: false Logical AND (x>10 && y<20): true

🧠 Bonus: Increment / Decrement Operator

Operator      Meaning
++        Add 1
--        Subtract 1
int a = 5; a++; // a = a + 1 => 6 a--; // a = 5

📝 Exercise for You (Try Now ✅)

Write a program:

Input:

a = 50 b = 20

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.

Theme images by 5ugarless. Powered by Blogger.