🚀 Labeled vs Unlabeled break in Java – Small Concept, Big Difference! When working with nested loops in Java, understanding the difference between labeled and unlabeled break is very important. 🔹 Unlabeled break Stops only the innermost loop. for(int i=0;i<3;i++){ for(int j=0;j<5;j++){ if(j==3){ break; // breaks only inner loop } System.out.println(i+" "+j); } } 👉 The outer loop continues execution. 🔹 Labeled break Stops the loop that has the label (even the outer loop). outerloop: for(int i=0;i<3;i++){ for(int j=0;j<5;j++){ if(j==3){ break outerloop; // breaks outer loop } System.out.println(i+" "+j); } } 👉 Both loops stop immediately. 💡 Simple understanding: Unlabeled break = Exit from a room Labeled break = Exit from the building This is especially useful while working with: ✔ Nested loops ✔ 2D arrays ✔ Search logic #Java #JavaProgramming #CoreJava #JavaDeveloper #Coding #InterviewPreparation #Developers #LearnToCode #TechLearning #break
Java Loop Break: Labeled vs Unlabeled
More Relevant Posts
-
𝗘𝘃𝗲𝗿 𝗻𝗼𝘁𝗶𝗰𝗲𝗱 𝘁𝗵𝗶𝘀? In Java, switch-case with Strings sometimes feels faster than if-else. At first, both look pretty similar. But internally, they don’t work the same way. 𝗪𝗶𝘁𝗵 𝗶𝗳-𝗲𝗹𝘀𝗲, 𝗝𝗮𝘃𝗮 𝗰𝗵𝗲𝗰𝗸𝘀 𝗲𝗮𝗰𝗵 𝗰𝗼𝗻𝗱𝗶𝘁𝗶𝗼𝗻 𝗼𝗻𝗲 𝗯𝘆 𝗼𝗻𝗲: if (str.equals("A")) else if (str.equals("B")) else if (str.equals("C")) So it keeps going until it finds a match. --- Switch-case does something smarter. Java converts the String into a hash and uses that to jump closer to the right case. So instead of checking everything sequentially, it narrows things down faster. --- That said… If you only have 2–3 conditions, it really doesn’t matter. The difference shows up when the number of conditions grows. --- I actually realized this while looking at a long if-else chain in one of our services 😄 --- The bigger takeaway? It’s not about memorizing syntax. It’s about understanding how things work under the hood. --- Have you ever come across something like this in Java? #java #javadeveloper #backenddevelopment #softwareengineering #coding #springboot #programming #developers #systemdesign #tech
To view or add a comment, sign in
-
🚀 Level Up Your Java: Enums + Varargs ✔️Ever wanted to write code that feels like a natural language? Let’s talk about two "oldies but goldies" in Java: Enums and Varargs. ❓The Why ✔️ Enums: Stop using "Magic Strings" or random integers. Use Enums to define a fixed set of constants (like Days, Statuses, or Roles). ✔️Varargs: Short for "Variable Arguments." It lets a method accept zero or multiple arguments without manually creating an array every time. 💻 The Code ✔️Check out how clean this PermissionManager looks: public class JavaTips { // Define clear, type-safe roles enum Role { ADMIN, EDITOR, VIEWER, GUEST } // Use Varargs (...) to check multiple roles at once public static void checkAccess(String user, Role... allowedRoles) { System.out.println("Checking access for: " + user); for (Role role : allowedRoles) { // Logic here System.out.println("Checking against: " + role); } } public static void main(String[] args) { // Look how clean this call is! // No 'new Role[] { ... }' needed. checkAccess("Alice", Role.ADMIN, Role.EDITOR); // Works with one argument too checkAccess("Bob", Role.VIEWER); } } 💡 The Pro-Tip ✔️ When using Varargs, always make it the last parameter in your method signature. ✔️Java needs to know exactly when the "variable" part starts! 🔑 Key Takeaways: 🔹 Readability: Your method calls look like sentences. 🔹 Safety: Enums prevent typos that happen with Strings. 🔹Flexibility: Varargs handle 1, 10, or 0 inputs gracefully. ❓ What’s your favorite "clean code" trick in Java? Let’s chat in the comments! 👇 #Java #Programming #CleanCode #SoftwareEngineering #Backend
To view or add a comment, sign in
-
-
⏳ Day 13 – 1 Minute Java Clarity – String Immutability in Java Strings look simple… but there’s something powerful happening behind the scenes 📌 What is String Immutability? In Java, once a String object is created, it cannot be changed. 👉 Instead of modifying the existing string, Java creates a new object. 📌 Example: String str = "Hello"; str.concat(" World"); System.out.println(str); 👉 Output: Hello ❌ (not "Hello World") 💡 Why? Because concat() creates a new object, but we didn’t store it. ✔ Correct way: str = str.concat(" World"); System.out.println(str); // Hello World ✅ 💡 Real-time Example: Think of a username in a system: String username = "user123"; username.toUpperCase(); Even after calling toUpperCase(), the original value stays "user123" unless reassigned. 📌 Why Strings are Immutable? ✔ Security (used in passwords, URLs) ✔ Thread-safe (no synchronization issues) ✔ Performance optimization using String Pool ⚠️ Important: Too many string modifications? Use StringBuilder instead. 💡 Quick Summary ✔ Strings cannot be modified after creation ✔ Operations create new objects ✔ Always reassign if you want changes 🔹 Next Topic → Type casting in Java Have you ever faced issues because of String immutability? 👇 #Java #JavaProgramming #Strings #CoreJava #JavaDeveloper #BackendDeveloper #Coding #Programming #SoftwareEngineering #LearningInPublic #100DaysOfCode #ProgrammingTips #1MinuteJavaClarity
To view or add a comment, sign in
-
-
🚀 Day 2 of my Java journey — Deep dive into Strings! Today I went beyond basic Strings and learned 3 powerful concepts: 📦 Arrays ✅ What is an array — storing multiple values in one variable ✅ How to declare and initialize an array ✅ Accessing elements using index (starts from 0!) ✅ Looping through arrays 🔤 String Constant Pool ✅ Java stores String literals in a special memory area called the String Constant Pool ✅ If two variables have the same value, they share ONE object — saves memory! ✅ String a = "Subodh" and String b = "Subodh" → both point to the same object ✅ Using new String() creates a separate object — avoid it! 🔨 StringBuilder ✅ Strings in Java are immutable — once created they cannot change ✅ Every time you do s = s + "text", a new object is created — wasteful! ✅ StringBuilder solves this — it modifies the SAME object ✅ Use it when you are building/changing strings frequently ✅ Fast and efficient for single-threaded programs 🔒 StringBuffer ✅ Same as StringBuilder but thread-safe ✅ Use when multiple threads are accessing the same string ✅ Slightly slower than StringBuilder but safer 💡 One line summary: String = immutable | StringBuilder = fast + mutable | StringBuffer = safe + mutable Every concept I learn makes me more confident as a developer. This is Day 2 of many! 💪 If you are learning Java too, let us connect and grow together! 🙏 #Java #JavaDeveloper #Strings #StringBuilder #StringBuffer #100DaysOfCode #LearningToCode #Programming #BackendDevelopment #TechCareer
To view or add a comment, sign in
-
🚀 Day 3/100 – Java Practice Challenge Continuing my #100DaysOfCode journey with another important core Java concept. 🔹 Topics Covered: Generics in Java Understanding type safety, reusability, and avoiding runtime errors. 💻 Practice Code: 🔸 Generic Class Example class Box { private T value; public void set(T value) { this.value = value; } public T get() { return value; } } 🔸 Usage Box intBox = new Box<>(); intBox.set(10); Box strBox = new Box<>(); strBox.set("Hello"); System.out.println(intBox.get()); // 10 System.out.println(strBox.get()); // Hello 📌 Key Learning: ✔ Generics provide compile-time type safety ✔ Avoid ClassCastException ✔ Help write reusable and clean code ⚠️ Important: • Use <?> for unknown types (wildcards) • Use for bounded types • Generics work only with objects, not primitives 🔥 Interview Insight: Generics use type erasure — type information is removed at runtime Widely used in collections like List, Map<K, V> 👉 Without Generics: List list = new ArrayList(); list.add("Java"); list.add(10); // No compile-time error ❌ #100DaysOfCode #Java #JavaDeveloper #Generics #CodingJourney #LearningInPublic #Programming
To view or add a comment, sign in
-
Understanding == vs .equals() in Java 🔍 As I start sharing on LinkedIn, I thought I'd kick things off with a fundamental Java concept that often trips up developers: the difference between == and .equals() **The == Operator:** → Compares memory addresses (reference equality) → Checks if two references point to the exact same object → Works for primitives by comparing actual values **The .equals() Method:** → Compares the actual content of objects → Can be overridden to define custom equality logic → Default implementation in Object class uses == (unless overridden) Here's a practical example: String str1 = new String("Java"); String str2 = new String("Java"); str1 == str2 → false (different objects in memory) str1.equals(str2) → true (same content) **Key Takeaway:** Use == for primitives and reference comparison. Use .equals() when you need to compare the actual content of objects. This fundamental concept becomes crucial when working with Collections, String operations, and custom objects in enterprise applications. What other Java fundamentals would you like me to cover? Drop your suggestions in the comments. #Java #Programming #SoftwareDevelopment #BackendDevelopment #CodingTips #JavaDeveloper
To view or add a comment, sign in
-
-
🚀 Day 5/100 — Loops in Java 🔁 Loops allow a program to repeat a block of code multiple times without writing the same code again and again. They are one of the most important concepts in programming. Java mainly provides three types of loops: 🔹 1. for Loop Used when the number of iterations is known. Syntax: for(initialization; condition; update){ // code to run } Example: for(int i = 1; i <= 5; i++){ System.out.println(i); } Output: 1 2 3 4 5 Here: i = 1 → start value i <= 5 → condition check i++ → increment after each iteration 🔹 2. while Loop Used when the number of iterations is unknown and depends on a condition. Example: int i = 1; while(i <= 5){ System.out.println(i); i++; } The loop runs as long as the condition is true. 🔹 3. do-while Loop Runs the block at least once, even if the condition is false. Example: int i = 1; do{ System.out.println(i); i++; }while(i <= 5); Difference: while → condition checked before execution do-while → condition checked after execution 🔹 Nested Loops A loop inside another loop is called a nested loop. Commonly used for pattern printing problems. Example: Triangle pattern for(int i = 1; i <= 5; i++){ for(int j = 1; j <= i; j++){ System.out.print("* "); } System.out.println(); } Output: * * * * * * * * * * * * * * * 🔴 Live Example — Inverted Triangle Pattern int rows = 5; for(int i = rows; i >= 1; i--){ for(int j = 1; j <= i; j++){ System.out.print("* "); } System.out.println(); } Output: * * * * * * * * * * * * * * * 🎯 Challenge: Write a program to print an inverted triangle pattern using nested loops. Drop your code in the comments 👇 #Java #CoreJava #100DaysOfCode #JavaLoops #ProgrammingJourney
To view or add a comment, sign in
-
-
🚀 Java Series – Day 1/30 📌 Topic: ArrayList in Java (Most Used Collection) 🔹 What is ArrayList? ArrayList is a dynamic array in Java. 👉 Its size grows automatically when elements are added. 🔹 Why use ArrayList? ✔ No fixed size limitation ✔ Easy to add & remove elements ✔ Widely used in real-world projects 🔹 Important Methods (Must Know) ➕ add() → Insert element ❌ remove() → Delete element 🔍 get() → Access element 📏 size() → Number of elements 🔹 Example ArrayList<String> list = new ArrayList<>(); list.add("Java"); list.add("Python"); System.out.println(list.get(0)); System.out.println(list.size()); 🔹 Important Point 👉 ArrayList stores only objects (not primitive directly) 👉 Internally uses a resizable array 💡 Key Takeaway ArrayList is one of the most asked topics in interviews and widely used in backend development. Consistency is the key 🔥 Day 1 complete ✅ What do you think about this? 👇 #Java #JavaDeveloper #Programming #BackendDevelopment #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🔍 Understanding String vs StringBuilder vs StringBuffer in Java 📌 1️⃣ String String objects are immutable, meaning once a string is created, its value cannot be changed. Any modification results in the creation of a new object in memory. Because of this immutability, strings are thread-safe by default and are stored in the String Pool for memory optimization. ✔ Best used for fixed or constant text values such as messages, configuration keys, or identifiers. 📌 2️⃣ StringBuilder StringBuilder is a mutable class, allowing modifications to the same object without creating new ones. This makes it much faster for frequent string operations like concatenation or modification. However, it is not thread-safe, which means it should be used mainly in single-threaded environments. ✔ Best used when performing multiple string modifications in loops or intensive operations. 📌 3️⃣ StringBuffer StringBuffer is also mutable, similar to StringBuilder, but it is synchronized, making it thread-safe. Because synchronization adds overhead, it is generally slower than StringBuilder. ✔ Best used in multi-threaded applications where multiple threads may modify the same string. 💡 Key Takeaway • Use String for immutable and constant values • Use StringBuilder for fast string manipulation in single-threaded programs • Use StringBuffer when thread safety is required Thank you Anand Kumar Buddarapu Sir for your guidance and motivation. Learning from you was really helpful! 🙏 Uppugundla Sairam sir Saketh Kallepu sir #Java #Programming #SoftwareDevelopment #JavaDeveloper #CodingConcepts #LearningJava
To view or add a comment, sign in
-
-
🚨 Stop confusing == 𝗼𝗽𝗲𝗿𝗮𝘁𝗼𝗿 with .𝗲𝗾𝘂𝗮𝗹𝘀() in Java! 🚨 If you’ve ever compared two Strings in Java and got unexpected results, you’re not alone 😅 Let’s break it down 👇 🔹 == 𝗼𝗽𝗲𝗿𝗮𝘁𝗼𝗿 • Compares references (memory addresses) • Checks if two variables point to the same object • Works fine for primitives, but tricky for objects 🔸 .𝗲𝗾𝘂𝗮𝗹𝘀() 𝗺𝗲𝘁𝗵𝗼𝗱 • Compares values (content) • Checks if two objects are logically equal • Can be overridden in classes for custom equality 💡 𝗘𝘅𝗮𝗺𝗽𝗹𝗲: 𝘚𝘵𝘳𝘪𝘯𝘨 𝘢 = 𝘯𝘦𝘸 𝘚𝘵𝘳𝘪𝘯𝘨("𝘑𝘢𝘷𝘢"); 𝘚𝘵𝘳𝘪𝘯𝘨 𝘣 = 𝘯𝘦𝘸 𝘚𝘵𝘳𝘪𝘯𝘨("𝘑𝘢𝘷𝘢"); 𝘚𝘺𝘴𝘵𝘦𝘮.𝘰𝘶𝘵.𝘱𝘳𝘪𝘯𝘵𝘭𝘯(𝘢 == 𝘣); // 𝘧𝘢𝘭𝘴𝘦 (𝘥𝘪𝘧𝘧𝘦𝘳𝘦𝘯𝘵 𝘰𝘣𝘫𝘦𝘤𝘵𝘴) 𝘚𝘺𝘴𝘵𝘦𝘮.𝘰𝘶𝘵.𝘱𝘳𝘪𝘯𝘵𝘭𝘯(𝘢.𝘦𝘲𝘶𝘢𝘭𝘴(𝘣)); // 𝘵𝘳𝘶𝘦 (𝘴𝘢𝘮𝘦 𝘤𝘰𝘯𝘵𝘦𝘯𝘵) 📌 TL;DR: Use == for reference comparison, and .equals() for value comparison. #Java #CodingTips #Programming #SoftwareEngineering #Learning #javadevelopers
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