"Java from Scratch: Understanding Operators in Java"

☕ Day 10 of my “Java from Scratch” Series – “Operators in Java” In Java, operators are used to perform operations between variables. We perform operations on variables (operands) instead of directly on values. 📘 Example: a + b; Here, ‘a’ and ‘b’ are “Operands”, and ‘+’ is the “Operator”. 🔹 Types of Operators in Java 1️⃣ Arithmetic Operators 2️⃣ Relational Operators 3️⃣ Assignment Operators 4️⃣ Unary Operators 5️⃣ Logical Operators 1. Arithmetic Operators: Operator Meaning + Addition - Subtraction * Multiplication / Division % Modulo (Remainder) 📘 Examples: 5 + 10 => 15 (addition) 10 - 5 => 5 (subtraction) 11 / 2 => 5 (quotient) 11 % 2 => 1 (remainder) 9 * 2 => 18 (multiplication) 🧩 String Concatenation: When we add two strings, concatenation happens. Eg: String add = "a" + "b"; ✅ Result: "ab" When we add an int value to a String, the int is converted to String automatically. int a = 5; String result = "ab" + a; ✅ Result: "ab5" If two int values are concatenated with a String, the numeric operation happens first, then the concatenation. int a = 5; int b = 20; System.out.println(a + b + "ab"); ✅ Result: "25ab" 💡 Java performs operations from left to right. ⚠️ A Few Important Points: ❌ You cannot subtract a number from a String. ✅ You can subtract a number from a char — because chars have ASCII values. Example: int b = 20; System.out.println('a' - b); // 97 - 20 = 77 💡 In short: Operators help us perform arithmetic, relational, logical, and assignment operations efficiently — and Java handles them from left to right. #Java #Programming #Coding #Learning #SoftwareEngineering #JavaDeveloper #Operators #JavaFromScratch #InterviewQuestions #Tech #ArithmeticOperatorsInJava #NeverGiveUp

To view or add a comment, sign in

Explore content categories