OPERATORS IN JAVA - SATISH THAWAN

Breaking

Breaking

Featured

Wednesday, 15 October 2025

OPERATORS IN JAVA

 

🧠 Java Operators — Full Explanation (Telugu + English)

🔹 Definition / నిర్వచనం

Operators అంటే మనం variables మరియు values పై operations చేయడానికి ఉపయోగించే చిహ్నాలు లేదా symbols.
In Java, operators are special symbols that perform operations on variables and values.

⚙️ Types of Operators in Java

Javaలో మొత్తం 7 main types of operators ఉంటాయి:

TypeExampleMeaning
1️⃣ Arithmetic Operators+, -, *, /, %గణిత క్రియలు చేయడానికి
2️⃣ Unary Operators++, --, +, -Single operand మీద పనిచేస్తాయి
3️⃣ Assignment Operators=, +=, -=, *=, /=, %=విలువలు assign చేయడానికి
4️⃣ Relational Operators==, !=, >, <, >=, <=Compare చేయడానికి
5️⃣ Logical Operators&&, ||, !Logic conditions check చేయడానికి
6️⃣ Bitwise Operators&, |, ^, ~, <<, >>, >>>Bits పై పనిచేస్తాయి
7️⃣ Ternary Operator? :Short if-else condition
8️⃣ Type Cast Operator(datatype)Type convert చేయడానికి

🧮 1️⃣ Arithmetic Operators

OperatorExampleResultExplanation
+10 + 515Addition
-10 - 55Subtraction
*10 * 550Multiplication
/10 / 52Division
%10 % 31Remainder

🧾 Example:

int a = 10, b = 3; 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

🔁 2️⃣ Unary Operators

OperatorDescriptionExampleResult
++Increment++aa = a + 1
--Decrement--aa = a - 1
+Positive sign+aNo change
-Negative sign-aChanges sign

🧾 Example:

int a = 5; System.out.println(++a); // 6 (pre-increment) System.out.println(a++); // 6 (post-increment), next a=7 System.out.println(--a); // 6 (pre-decrement)

📦 3️⃣ Assignment Operators

OperatorExampleEquivalent To
=a = 10a = 10
+=a += 5a = a + 5
-=a -= 5a = a - 5
*=a *= 5a = a * 5
/=a /= 5a = a / 5
%=a %= 5a = a % 5

🧾 Example:

int a = 10; a += 5; // a = 15

⚖️ 4️⃣ Relational (Comparison) Operators

OperatorMeaningExampleResult
==Equal to10 == 10true
!=Not equal to10 != 5true
>Greater than10 > 5true
<Less than10 < 5false
>=Greater or equal10 >= 10true
<=Less or equal5 <= 10true

🧾 Example:

int x = 10, y = 20; System.out.println(x > y); // false System.out.println(x <= y); // true

🔍 5️⃣ Logical Operators

OperatorMeaningExampleResult
&&Logical AND(x>5 && y<10)true if both true
||Logical OR(x>5 || y<10)true if any one true
!Logical NOT!(x>5)opposite value

🧾 Example:

int a = 10, b = 20; System.out.println(a>5 && b>15); // true System.out.println(a>15 || b>15); // true System.out.println(!(a>5)); // false

⚡ 6️⃣ Bitwise Operators

OperatorMeaningExampleResult
&AND5 & 31
|OR5 | 37
^XOR5 ^ 36
~NOT~5-6
<<Left shift5 << 110
>>Right shift5 >> 12
>>>Unsigned right shift-5 >>> 1large positive value

❓ 7️⃣ Ternary Operator

Syntax:

variable = (condition) ? value_if_true : value_if_false;

🧾 Example:

int age = 18; String result = (age >= 18) ? "Eligible" : "Not Eligible"; System.out.println(result);

Output:

Eligible

🔄 8️⃣ Type Cast Operator

Used to convert one data type to another.

🧾 Example:

double d = 10.5; int i = (int)d; // type casting System.out.println(i); // 10

🧠 Small Interview Questions

1️⃣ What is the difference between == and =?
➡️ == is comparison operator, = is assignment operator.

2️⃣ What is the difference between && and &?
➡️ && is logical AND (used with boolean), & is bitwise AND (used with bits).

3️⃣ What is the output of 5 + 2 * 3?
➡️ 11 (Multiplication has higher precedence)

4️⃣ What is a ternary operator used for?
➡️ Used as a short form of if-else.

There are 8 main types of operators in Java, and here’s a clear, structured explanation of each — with syntax, examples, and outputs 👇


💡 Types of Operators in Java

No.Operator TypeDescription
1️⃣Unary OperatorWorks on a single operand (like ++, --, +, -)
2️⃣Arithmetic OperatorPerforms mathematical operations (+, -, *, /, %)
3️⃣Relational OperatorCompares two values (==, !=, >, <, >=, <=)
4️⃣Ternary OperatorShortcut for if-else condition (? :)
5️⃣Assignment OperatorAssigns values (=, +=, -=, *=, /=, %=)
6️⃣Bitwise OperatorPerforms bit-level operations (&,
7️⃣Logical OperatorWorks on boolean values (&&, ||, !)
8️⃣Shift OperatorShifts bits left or right (<<, >>, >>>)

1️⃣ Unary Operators

Used with one operand.

OperatorMeaningExampleOutput
++Increment++aIncreases value by 1
--Decrement--aDecreases value by 1
+Unary plus+aIndicates positive
-Unary minus-aNegates the value

Example:

int a = 5; System.out.println(++a); // 6 System.out.println(a--); // 6 (then a becomes 5)

2️⃣ Arithmetic Operators

Used for mathematical calculations.

OperatorExampleResult
+10 + 515
-10 - 55
*10 * 550
/10 / 33
%10 % 31

Example:

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

3️⃣ Relational Operators

Used to compare two values.

OperatorMeaningExampleOutput
==Equal toa == btrue / false
!=Not equal toa != btrue / false
>Greater thana > btrue / false
<Less thana < btrue / false
>=Greater or equala >= btrue / false
<=Less or equala <= btrue / false

Example:

int x = 10, y = 20; System.out.println(x > y); // false

4️⃣ Ternary Operator

Used for conditional checking in a single line.

Syntax:

variable = (condition) ? value_if_true : value_if_false;

Example:

int age = 18; String result = (age >= 18) ? "Adult" : "Minor"; System.out.println(result); // Adult

5️⃣ Assignment Operators

Used to assign values to variables.

OperatorExampleEquivalent To
=a = 5a = 5
+=a += 5a = a + 5
-=a -= 5a = a - 5
*=a *= 5a = a * 5
/=a /= 5a = a / 5
%=a %= 5a = a % 5

Example:

int a = 10; a += 5; // a = 15

6️⃣ Bitwise Operators

Operate on bits (0s and 1s).

OperatorMeaningExampleResult
&AND5 & 31
|OR5 | 37
^XOR5 ^ 36
~NOT~5-6

Example:

int a = 5, b = 3; System.out.println(a & b); // 1

7️⃣ Logical Operators

Used with boolean (true/false) values.

OperatorMeaningExampleResult
&&AND(a>5 && b>10)true if both true
||OR(a>5 || b>10)true if one is true
!NOT!(a>5)reverses result

Example:

int a = 10, b = 20; System.out.println(a>5 && b>15); // true

8️⃣ Shift Operators

Used to shift bits left or right.

OperatorMeaningExampleResult
<<Left Shift10 << 120
>>Right Shift10 >> 15
>>>Unsigned Right Shift-10 >>> 1large positive number

Example:

int a = 10; System.out.println(a << 1); // 20 System.out.println(a >> 1); // 5

⚙️ Operator Precedence (High → Low)

PriorityOperator
1++, --
2*, /, %
3+, -
4<<, >>, >>>
5<, <=, >, >=
6==, !=
7&
8^
9|
10&&
11||
12?:
13=, +=, -=, *=, /=, %=

Food