☕ 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
"Java from Scratch: Understanding Operators in Java"
More Relevant Posts
-
💡 Difference between == and .equals() in Java — and why it still confuses even experienced devs In Java, many developers think == and .equals() do the same thing... but they don’t 👇 ⚙️ == — The == operator compares memory references. It checks whether two variables point to the same object. String a = new String("Java"); String b = new String("Java"); System.out.println(a == b); // false 🚫 Here, a and b are two different objects, even if their content is identical. 🧠 .equals() — The .equals() method compares the logical content of the objects (when properly implemented). System.out.println(a.equals(b)); // true ✅ Both Strings contain “Java”, so the result is true. 🧩 Extra tip: Primitive types (int, double, boolean, etc.) use == because they don’t have .equals(). Objects (String, Integer, List, etc.) should use .equals() unless you need to check if they’re the same object in memory. 💬 Conclusion: Use == ➡️ to compare references Use .equals() ➡️ to compare values 💭 Have you ever fallen into this trap? Share your experience below 👇 #Java #Backend #CleanCode #DeveloperTips #SpringBoot #Programming #Learning
To view or add a comment, sign in
-
💡 Difference Between String and StringBuffer in Java :- In Java, both String and StringBuffer are used to handle text data — but they differ in how they manage mutability and performance. 🔹 String : Immutable → Once created, its value cannot be changed. Every modification (like concatenation) creates a new object in memory. Less efficient when performing frequent modifications. Example : String s = "Java"; s = s + " Programming"; // Creates a new object 🔸 StringBuffer : Mutable → Can be modified directly without creating new objects. Best for multiple string manipulations (append, insert, reverse, etc.). Thread-safe → Methods are synchronized. Example: StringBuffer sb = new StringBuffer("Java"); sb.append(" Programming"); // Modifies the same object ✨ In Short : 🔹 String → Immutable and memory-consuming when modified. 🔹 StringBuffer → Mutable and efficient for frequent string operations. Special thanks to my mentors Anand Kumar Buddarapufor helping me understand Java’s memory handling and performance optimization concepts more clearly. #Java #String #StringBuffer #ProgrammingConcepts #Codegnan #Mentorship
To view or add a comment, sign in
-
-
🧠 Day 8: Java Loops Today’s focus is on loops in Java — how we make programs repeat tasks efficiently. 💡 What I Learned Today for loop – best for known number of iterations while loop – runs while a condition is true do-while loop – executes once, then checks condition for-each loop – used to iterate through arrays or collections Avoid infinite loops by ensuring your condition eventually becomes false 🧩 Example Code public class LoopExample { public static void main(String[] args) { // For loop for (int i = 1; i <= 5; i++) { System.out.println("For Loop: " + i); } // While loop int j = 1; while (j <= 3) { System.out.println("While Loop: " + j); j++; } // Do-While loop int k = 1; do { System.out.println("Do-While Loop: " + k); k++; } while (k <= 2); } } 🗣️ Caption for LinkedIn 🔁 Day 8 of my #30DaysOfJava challenge! Today I explored Loops in Java — the power of repetition that makes programs dynamic and efficient. Mastering loops = writing less code and achieving more! 💡 #Java #CodingJourney #CoreJava #LearnJava #Programming
To view or add a comment, sign in
-
-
🎯 Java Generics — Why They Matter If you’ve been writing Java, you’ve probably used Collections like List, Set, or Map. But have you ever wondered why List<String> is safer than just List? That’s Generics in action. What are Generics? Generics let you parameterize types. Instead of working with raw objects, you can define what type of object a class, method, or interface should work with. List<String> names = new ArrayList<>(); names.add("Alice"); // names.add(123); // ❌ Compile-time error Why use Generics? 1. Type Safety – Catch errors at compile-time instead of runtime. 2. Code Reusability – Write flexible classes and methods without losing type safety. 3. Cleaner Code – No need for casting objects manually. public <T> void printArray(T[] array) { for (T element : array) { System.out.println(element); } } ✅ Works with Integer[], String[], or any type — one method, many types. Takeaway Generics aren’t just syntax sugar — they make your Java code safer, cleaner, and more reusable. If you’re still using raw types, it’s time to level up! 🚀 ⸻ #Java #SoftwareEngineering #ProgrammingTips #Generics #CleanCode #TypeSafety #BackendDevelopment
To view or add a comment, sign in
-
☕ Day 16 of my “Java from scratch” series “Dynamic Inputs in Java” 🎯 What is it? The values for variables can be decided at runtime. For this, we use the Scanner class. 🧩 Syntax: Scanner sc = new Scanner(System.in); int value1 = sc.nextInt(); String string = sc.nextLine(); String string2 = sc.next(); ⚠️ Note: There is a small issue in Java when taking inputs of different types (like int and String) one after another. If we take an int input first and then a String input immediately, the string input might be skipped 😅 👉 This happens because after entering the integer, we press Enter, and that newline character (\n) is still left in the input buffer. So when the next input (like nextLine()) is called, it reads that leftover newline instead of waiting for new input. ✅ Solution: To fix this, we add an extra sc.nextLine() to consume the leftover newline character before reading the next string. 🧠 Example: Scanner sc = new Scanner(System.in); System.out.print("Enter an integer: "); int value1 = sc.nextInt(); sc.nextLine(); // consume the leftover newline System.out.print("Enter a string: "); String string = sc.nextLine(); System.out.println("Integer: " + value1); System.out.println("String: " + string); 💡 Key takeaway: Always remember to handle newline characters properly when using Scanner for mixed inputs — it’s one of those small details that separates a beginner from a smart Java developer 😎 #Java #Programming #JavaBeginners #Coding #SoftwareDevelopment #JavaFromScratch #LearnJava #Tech #InterviewQuestions #NeverGiveUp
To view or add a comment, sign in
-
💡 Understanding Interfaces in Java: 1)In Java, an Interface is a blueprint of a class. 2)It defines abstract methods that must be implemented by the class that uses it. 3)Interfaces help achieve abstraction, polymorphism, and multiple inheritance. 🧩 Example: interface Vehicle { void start(); void stop(); } class Car implements Vehicle { public void start() { System.out.println("Car started"); } public void stop() { System.out.println("Car stopped"); } } ⚙ Types of Interfaces in Java: 1️⃣ Normal Interface 👉 Contains two or more abstract methods. Used commonly in real-world applications. 2️⃣ Functional Interface 👉 Contains only one abstract method. (Example: Runnable, Comparable) ✅ Used in Lambda Expressions. 3️⃣ Marker Interface 👉 Has no methods or fields. Used to mark or tag a class. (Example: Serializable, Cloneable). 4️⃣ SAM Interface (Single Abstract Method) 👉 Another name for a Functional Interface — ensures only one abstract method exists. 💬 Why Use Interfaces? ✔ Promotes loose coupling ✔ Makes code scalable and flexible ✔ Enables multiple inheritance #Java #OOP #Interface #Programming #Coding #TechLearning #JavaDeveloper #SoftwareEngineering
To view or add a comment, sign in
-
🚀 Constructor vs Method in Java – A Must-Know Difference for Every Developer! When you dive deeper into Java, one of the most fundamental yet commonly misunderstood concepts is the difference between a Constructor and a Method. Both may look similar — they can have parameters, perform actions, and even look almost identical in syntax — but their purpose and behavior are quite different 👇 🔹 Constructor 👉Used to initialize objects. 👉Has the same name as the class. 👉No return type, not even void. 👉Automatically invoked when an object is created. 🔹 Method 👉Used to define behavior or functionality of an object. 👉Can have any name (except the class name). 👉Always has a return type (or void). 👉Invoked explicitly after object creation. Here’s a simple and clear example 👇 class Car { String model; int year; // Constructor Car(String model, int year) { thismodel = model; this.year = year; System.out.println("Car object created!"); } // Method void displayDetails() { System.out.println("Model: " + model + ", Year: " + year); } public static void main(String[] args) { Car c1 = new Car("Tesla Model 3", 2024); // Constructor called c1.displayDetails(); // Method called } } ✅ Key Takeaway: Think of a constructor as giving life to an object, while a method defines what that object can do once it’s alive! #Java #OOP #ProgrammingConcepts #LearnJava #CodeBetter #SoftwareDevelopment #JavaDevelopers
To view or add a comment, sign in
-
-
☕ Day 18 of my “Java from Scratch” series — “Command Line Arguments in Java” Ever wondered how we can pass inputs directly while running a Java program? 🤔 That’s where Command Line Arguments come in! 📘 What are Command Line Arguments? We can pass arguments to our program at runtime through the terminal. 🧩 Syntax: java ClassName argument1 argument2 ✅ Example: java HelloWorld Hi Java 🧾 Output: Hi Java 💡 Code: public class HelloWorld { public static void main(String[] args) { System.out.println(args[0]); System.out.println(args[1]); } } 🧠 Real-time Use Case: In real-world applications, we pass arguments like port numbers, log levels, and config paths to the JVM while running the application. ⚙️ How to pass arguments in Eclipse IDE: 1️⃣ Right-click on your main class 2️⃣ Go to Run As → Run Configurations 3️⃣ Select your class 4️⃣ Click Arguments on the right 5️⃣ Enter your program arguments 6️⃣ Click Run 🚀 📌 Note: Even if we pass numbers as arguments, Java treats them as Strings. #Java #Programming #JavaFromScratch #LearningSeries #Developers #Coding #CommandLineArgumentsInJava #Tech #SoftwareDeveloper #JavaLearning #InterviewConceptsInJava #NeverGiveUp
To view or add a comment, sign in
-
📘 Topics Covered: - What is a Prime Number - Java Program to check Prime Number - Using Loops & Conditions in Java - Beginner Java Concepts import java.util.Scanner; public class PrimeNumber { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); int n = sc.nextInt(); boolean isPrime = true; if (n <= 1) { isPrime = false; } else { for (int i = 2; i <= n / 2; i++) { if (n % i == 0) { isPrime = false; break; } } } if (isPrime) System.out.println(n + " is a Prime Number"); else System.out.println(n + " is not a Prime Number"); sc.close(); } } #JavaProgram #PrimeNumber #JavaTutorial #CodingForBeginners #SoftwareTestingbyKP
To view or add a comment, sign in
-
-
💡 Java — String, SCP & Heap Memory (Complete Guide) In Java, String is one of the most commonly used and powerful classes — but understanding how it works in memory helps us write more efficient code. Let’s break it down simply 👇 🔹 1️⃣ What is a String? A String in Java is an immutable (unchangeable) sequence of characters. Example: String name = "KodeWala"; Once created, the value of a String cannot be modified. 🔹 2️⃣ Memory Allocation — SCP vs Heap 🧩 String Constant Pool (SCP): Part of the Method Area in JVM. Stores unique string literals. If the same literal is used again, Java reuses it (no duplicate object). Example: String s1 = "Java"; String s2 = "Java"; System.out.println(s1 == s2); // ✅ true (same object in SCP) 🧠 Heap Memory: When you create a string using new, it’s stored in heap memory. String s3 = new String("Java"); System.out.println(s1 == s3); // ❌ false (different object) 🔹 3️⃣ The intern() Method 👉 intern() helps to move or link a heap string to SCP. If the same literal exists in SCP, it returns that reference; otherwise, it adds the string to SCP and returns it. Example: String s4 = new String("Hello").intern(); String s5 = "Hello"; System.out.println(s4 == s5); // ✅ true 🧩 Why use intern()? ✅ Saves memory ✅ Improves performance in String-heavy applications 💬 Quick Summary: SCP → Stores unique literals Heap → Stores objects created using new intern() → Links heap object with SCP ✨ In short: Understanding how Java stores and manages Strings helps you write cleaner and more memory-efficient code. #Java #String #MemoryManagement #Developers #LearningEveryday #JavaTips #Programming #InterviewPrep #TechInsights
To view or add a comment, sign in
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development