Operators in Java are symbols used to perform operations on variables and values. Java supports a rich set of operators used in expressions to manipulate data and control program logic.


🧮 What is an Operator?

An operator performs a specific action on one or more operands.

java
int sum = 10 + 5; // '+' is an operator

🗂️ Categories of Java Operators

CategoryDescription
ArithmeticBasic math operations
Relational (Comparison)Compare two values
LogicalCombine boolean expressions
AssignmentAssign values
UnaryOperate on a single operand
BitwiseOperate on bits
TernaryShort form of if-else
ShiftShift bits left/right

1️⃣ Arithmetic Operators

OperatorDescriptionExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulus (remainder)a % b
java
int x = 10, y = 3; System.out.println(x % y); // Output: 1

2️⃣ Relational (Comparison) Operators

OperatorDescriptionExample
==Equal toa == b
!=Not equal toa != b
>Greater thana > b
<Less thana < b
>=Greater than or equal toa >= b
<=Less than or equal toa <= b
java
System.out.println(5 > 3); // true

3️⃣ Logical Operators

OperatorDescriptionExample
&&Logical ANDa > 0 && b > 0
``
!Logical NOT!(a > 0)
java
boolean result = (5 > 3) && (8 > 5); // true

4️⃣ Assignment Operators

OperatorExampleEquivalent To
=a = 10assign 10 to a
+=a += 5a = a + 5
-=a -= 3a = a - 3
*=a *= 2a = a * 2
/=a /= 4a = a / 4
%=a %= 2a = a % 2

5️⃣ Unary Operators

OperatorDescriptionExample
+Positive+a
-Negative-a
++Incrementa++ or ++a
--Decrementa-- or --a
!Boolean NOT!true = false

6️⃣ Ternary Operator

Shorthand for if-else.

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

7️⃣ Bitwise Operators (Advanced)

OperatorDescriptionExample
&ANDa & b
``OR
^XORa ^ b
~NOT~a
<<Left shifta << 2
>>Right shifta >> 2

Used mostly in low-level or performance-sensitive code.


🧪 Code Example

java
public class OperatorExample { public static void main(String[] args) { int a = 10, b = 5; System.out.println("Sum: " + (a + b)); System.out.println("Is a > b? " + (a > b)); System.out.println("Is either a > b or b > 10? " + (a > b || b > 10)); } }

🧾 Summary Table

TypeOperators
Arithmetic+, -, *, /, %
Relational==, !=, >, <, >=, <=
Logical&&, `
Assignment=, +=, -=, etc.
Unary++, --, -, +, !
Ternarycondition ? true : false
Bitwise&, `