"Unary Operators in Java: Understanding the Basics"

☕ Day 13 of my “Java from Scratch” Series – “Unary Operators in Java” Unary operators are the ones that work on a single operand (variable). They’re used to change the sign, increase/decrease a value, or reverse a boolean condition. 1️⃣ +a 2️⃣ -a → if a = 5, -a will be -5 3️⃣ ! → if a = true, !a will be false 4️⃣ Increment operator (++) → increases the value by 1. a. Post increment: first the operation will be performed and then the value will be increased. 📘 Example: int a = 20; int b = a++; ✅ Result: b = 20, a = 21 b. Pre increment: first the value will be increased and then the operation will be performed. 📘 Example: int a = 20; int b = ++a; ✅ Result: a = 21, b = 21 5️⃣ Decrement operator (--) → decreases the value by 1. a. Post decrement: first the operation will be performed and then the value will be decremented. 📘 Example: int a = 20; int b = a--; ✅ Result: b = 20, a = 19 b. Pre decrement: first the value will be decremented and then the operation will be performed. 📘 Example: int a = 20; int b = --a; ✅ Result: a = 19, b = 19 💡 In short: Unary operators act on one operand to either change its value or its sign. 👉 Which one do you find tricky — pre or post increment? Comment below 👇 #Java #Programming #Coding #Tech #JavaDeveloper #SoftwareEngineering #Learning #Developers #UnaryOperatorsInJava #JavaFromScratch #NeverGiveUp

To view or add a comment, sign in

Explore content categories