𝗝𝗮𝘃𝗮’𝘀 𝗠𝗼𝘀𝘁 𝗔𝗻𝗻𝗼𝘆𝗶𝗻𝗴 𝗢𝘃𝗲𝗿𝗹𝗼𝗮𝗱: List.remove(index) 𝘃𝘀 remove(value) ⚠️ 𝗦𝗮𝗺𝗲 𝗺𝗲𝘁𝗵𝗼𝗱 - 𝗱𝗶𝗳𝗳𝗲𝗿𝗲𝗻𝘁 𝗺𝗲𝗮𝗻𝗶𝗻𝗴 🤯 remove(int index) vs remove(Object o) Tiny type change - different behavior. 𝗪𝗵𝗲𝗿𝗲 𝗶𝘁 𝗯𝗿𝗲𝗮𝗸𝘀 🔥 List<Integer> list = new ArrayList<>(List.of(10,20,30)); list.remove(1); // index -> removes 20 Integer i = 1; list.remove(i); // object -> tries to remove 1 𝗦𝗮𝗺𝗲 𝗻𝘂𝗺𝗯𝗲𝗿. 𝗗𝗶𝗳𝗳𝗲𝗿𝗲𝗻𝘁 𝗿𝗲𝘀𝘂𝗹𝘁 💥 You think index. Java sees object. 𝗡𝗼𝘁 𝗼𝗻𝗹𝘆 𝗹𝗼𝗴𝗶𝗰 - 𝗮𝗹𝘀𝗼 𝗰𝗼𝘀𝘁 🐢 remove(index) - no search remove(value) - extra O(n) lookup 𝗪𝗵𝘆 𝗶𝘁 𝗵𝘂𝗿𝘁𝘀 😤 silent bug same-looking code wrong element or nothing removed 𝗔𝗱𝘃𝗶𝗰𝗲 ✅ Make intent explicit: remove((int) i) or remove(Integer.valueOf(x)) Never rely on overloads in critical paths. #java #backend #softwareengineering #coding #programming #javatips #cleancode #bug #performance
Ruslan Mukhamadiarov’s Post
More Relevant Posts
-
𝗗𝗮𝘆 𝟯: 𝗣𝗶𝘃𝗼𝘁𝗶𝗻𝗴 𝘄𝗵𝗲𝗻 𝘁𝗵𝗲 𝘀𝗶𝗺𝗽𝗹𝗲 𝗮𝗽𝗽𝗿𝗼𝗮𝗰𝗵 𝗳𝗮𝗶𝗹𝘀. 🧠 The screenshot doesn’t show the struggle, It just shows the final Accepted code. ✅𝗙𝗼𝗿 𝗗𝗮𝘆 𝟯, I tackled this LeetCode problem: "𝟮𝟴𝟯𝟵. 𝗖𝗵𝗲𝗰𝗸 𝗶𝗳 𝗦𝘁𝗿𝗶𝗻𝗴𝘀 𝗖𝗮𝗻 𝗯𝗲 𝗠𝗮𝗱𝗲 𝗘𝗾𝘂𝗮𝗹 𝗪𝗶𝘁𝗵 𝗢𝗽𝗲𝗿𝗮𝘁𝗶𝗼𝗻𝘀 𝗜." My goal was simple: just get that Java solution 𝗔𝗰𝗰𝗲𝗽𝘁𝗲𝗱. 𝗧𝗵𝗲 𝗙𝗶𝗿𝘀𝘁 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵: I initially started with a classic iterative method, trying to loop through and swap indices to check equality. But I kept hitting unexpected logic errors that were overcomplicating a simple problem. I realized the "obvious" way wasn't the "correct" way. 𝗧𝗵𝗲 𝗣𝗶𝘃𝗼𝘁: Instead of getting stuck on the iteration, I looked at the core constraint. The operation only allows swaps between characters exactly 2 indices apart (e.g., indices 0 and 2, 1 and 3). 𝗧𝗵𝗲 𝗕𝗿𝗲𝗮𝗸𝘁𝗵𝗿𝗼𝘂𝗴𝗵: I realized that if strings are length 4, characters at even indices (0, 2) can only ever interact with each other. The same applies to odd indices (1, 3). So, instead of looping, I just needed to compare those two specific pairs directly. ✅𝗧𝗵𝗲 𝗿𝗲𝘀𝘂𝗹𝘁: I simplified the code into two simple comparison blocks and hit Submit. That final 𝟭𝗺𝘀 𝗥𝘂𝗻𝘁𝗶𝗺𝗲 (𝗕𝗲𝗮𝘁𝗶𝗻𝗴 𝟵𝟵.𝟲𝟲%) was the perfect confirmation that the pivot was the right choice. Sometimes the best engineering decision is to tear down what isn't working and start with a cleaner logic. #Java #JavaDeveloper #DSA #Strings #BuildInPublic #EngineeringStudent #DAY3
To view or add a comment, sign in
-
-
Recently revisited an important Java Streams concept: reduce() - one of the most elegant terminal operations for aggregation. Many developers use loops for summing or combining values, but reduce() brings a functional and expressive approach. Example: List<Integer> nums = List.of(1, 2, 3, 4); int sum = nums.stream() .reduce(0, Integer::sum); What happens internally? reduce() repeatedly combines elements: 0 + 1 = 1 1 + 2 = 3 3 + 3 = 6 6 + 4 = 10 Why it matters: ✔ Cleaner than manual loops ✔ Great for immutable / functional style code ✔ Useful for sum, max, min, product, concatenation, custom aggregation ✔ Common in backend processing pipelines Key Insight: Integer::sum is just a method reference for: (a, b) -> a + b Small concepts like this make code more readable and scalable. Still amazed how much depth Java Streams offer beyond just filter() and map(). #Java #Programming #BackendDevelopment #SpringBoot #JavaStreams #Coding #SoftwareEngineering
To view or add a comment, sign in
-
💡 𝗝𝗮𝘃𝗮/𝐒𝐩𝐫𝐢𝐧𝐠 𝐁𝐨𝐨𝐭 𝗧𝗶𝗽 - 𝗦𝘄𝗶𝘁𝗰𝗵 𝗘𝘅𝗽𝗿𝗲𝘀𝘀𝗶𝗼𝗻 💎 🕯 𝗧𝗿𝗮𝗱𝗶𝘁𝗶𝗼𝗻𝗮𝗹 𝗦𝘄𝗶𝘁𝗰𝗵 𝗦𝘁𝗮𝘁𝗲𝗺𝗲𝗻𝘁 The traditional switch statement has been part of Java since the beginning. It requires explicit break statements to prevent fall-through, which can lead to bugs if forgotten. Each case must contain statements that execute sequentially, making the code verbose and error-prone. 💡 𝗠𝗼𝗱𝗲𝗿𝗻 𝗦𝘄𝗶𝘁𝗰𝗵 𝗘𝘅𝗽𝗿𝗲𝘀𝘀𝗶𝗼𝗻 Switch expressions were introduced in Java 14 as a more concise and safe alternative. Using the -> syntax, you eliminate the need for break statements and can directly return values. Multiple cases can be grouped with commas, and the compiler enforces exhaustiveness for better safety. ✅ 𝗞𝗲𝘆 𝗕𝗲𝗻𝗲𝗳𝗶𝘁𝘀 ◾ No break statements, safer and cleaner code. ◾ Direct value assignment, treat switch as an expression. ◾ Multiple labels with comma separation. ◾ Compiler exhaustiveness checks, fewer runtime errors. 🤔 Which one do you prefer? #java #springboot #programming #softwareengineering #softwaredevelopment
To view or add a comment, sign in
-
-
𝑾𝒉𝒂𝒕 𝒂𝒄𝒕𝒖𝒂𝒍𝒍𝒚 𝒉𝒂𝒑𝒑𝒆𝒏𝒔 𝒘𝒉𝒆𝒏 𝑱𝑽𝑴 𝒓𝒖𝒏𝒔 𝒚𝒐𝒖𝒓 𝑱𝒂𝒗𝒂 𝒑𝒓𝒐𝒈𝒓𝒂𝒎 ? 𝐖𝐞 𝐚𝐥𝐥 𝐰𝐫𝐢𝐭𝐞 "𝐦𝐚𝐢𝐧()" 𝐦𝐞𝐭𝐡𝐨𝐝 𝐁𝐮𝐭 𝐡𝐚𝐯𝐞 𝐲𝐨𝐮 𝐞𝐯𝐞𝐫 𝐭𝐡𝐨𝐮𝐠𝐡𝐭 𝐰𝐡𝐚𝐭 𝐉𝐕𝐌 𝐝𝐨𝐞𝐬 𝐢𝐧𝐭𝐞𝐫𝐧𝐚𝐥𝐥𝐲 𝐭𝐨 𝐬𝐭𝐚𝐫𝐭 𝐲𝐨𝐮𝐫 𝐩𝐫𝐨𝐠𝐫𝐚m ? 🔍 Let’s break it down simply 📝 𝐒𝐭𝐞𝐩-𝐛𝐲-𝐒𝐭𝐞𝐩 𝐄𝐱𝐞𝐜𝐮𝐭𝐢𝐨𝐧 1️⃣ Class Loading ▫️ JVM loads the class into memory 2️⃣ Method Lookup ▫️ JVM looks for exact method signature: 𝐩𝐮𝐛𝐥𝐢𝐜 𝐬𝐭𝐚𝐭𝐢𝐜 𝐯𝐨𝐢𝐝 𝐦𝐚𝐢𝐧(𝐒𝐭𝐫𝐢𝐧𝐠[] 𝐚𝐫𝐠𝐬) ▫️ Signature must match exactly 3️⃣ No Object Creation ▫️JVM does NOT create an object ▫️Because "main()" is static ▫️It is called using class name directly 4️⃣ Method Invocation ▫️ JVM directly calls 5️⃣ Execution Starts ▫️ Program execution begins from "main()" 🧭 𝐈𝐦𝐩𝐨𝐫𝐭𝐚𝐧𝐭 𝐈𝐧𝐬𝐢𝐠𝐡𝐭 "main()" is the entry point of Java program ▫️JVM specifically looks for this method ▫️ If not found → program will not run ❌ 🔁 𝐋𝐨𝐚𝐝 𝐂𝐥𝐚𝐬𝐬 → 𝐅𝐢𝐧𝐝 𝐦𝐚𝐢𝐧() → 𝐂𝐚𝐥𝐥 𝐝𝐢𝐫𝐞𝐜𝐭𝐥𝐲 → 𝐄𝐱𝐞𝐜𝐮𝐭𝐞 ♣️ We write just one method but JVM does a lot behind the scenes to run it. 🎈Did you know this flow before? Or just wrote "main()" without thinking much? Let’s discuss 💬 #Java #JVM #JavaDeveloper #Programming #TechJourney #LearnBySharing #JavaConcepts #InterviewPrep #BackendDevelopment
To view or add a comment, sign in
-
𝗢𝗽𝘁𝗶𝗼𝗻𝗮𝗹.𝗼𝗿𝗘𝗹𝘀𝗲() 𝗘𝘅𝗲𝗰𝘂𝘁𝗲𝘀 𝗘𝘃𝗲𝗻 𝗪𝗵𝗲𝗻 𝗩𝗮𝗹𝘂𝗲 𝗣𝗿𝗲𝘀𝗲𝗻𝘁 ⚠️ Looks harmless. It’s not. User user = findUserInCache(userId) .orElse(loadUserFromDatabase(userId)); Many developers read this as: 👉 "load from DB only if cache missed" But that is not what happens. 🔥 What actually happens orElse(...) is eager. That means the argument is evaluated before orElse() is called. So even when user is already present in cache, this still runs: 👉 loadUserFromDatabase(userId) Effectively: User fallback = loadUserFromDatabase(userId); // always executed User user = findUserInCache(userId).orElse(fallback); 💥 Why this hurts in production That fallback is often not cheap. It can be: 🔹 DB query 🔹 remote API call 🔹 disk read 🔹 heavy object construction So what looked like a safe fallback becomes hidden work on every request. 👉 extra CPU 👉 unnecessary IO 👉 more latency 👉 performance regression that is easy to miss in review ✅ The correct approach Use orElseGet(...) when fallback is expensive. User user = findUserInCache(userId) .orElseGet(() -> loadUserFromDatabase(userId)); Now the fallback runs only if Optional is empty. ⚠️ When orElse() is fine orElse() is still okay when the fallback is trivial: String name = maybeName.orElse("unknown"); Constant value, already computed value, or something very cheap. 🧠 Takeaway orElse() is not lazy. If you pass a method call there, that method may execute even when the value is already present. That is exactly the kind of tiny thing that later turns into: "Why is the DB getting hit so much?" 🤦 Have you ever seen a small Java convenience API cause a very non-convenient production problem? 👀 #java #springboot #performance #backend #softwareengineering #programming #coding
To view or add a comment, sign in
-
-
🚨 Java fact about constructors 👇 Constructors don’t have a return type… But something still returns an object 🤯 Example: class User { User() { System.out.println("Constructor called"); } } User user = new User(); 👉 What’s actually happening? - "new" keyword creates the object - Allocates memory - Calls the constructor - Returns the reference 👉 Important: Constructor itself does NOT return anything 👉 "new" keyword returns the object --- 👉 This is NOT a constructor: class User { void User() { } ❌ } 👉 It becomes a normal method 💡 Many people think constructor returns object… but actually new keyword does that Did you know this? 👇 #Java #CoreJava #Programming #BackendDeveloper #Coding #TechLearning
To view or add a comment, sign in
-
This Java snippet trips up even experienced developers 👇 Integer a = 127; Integer b = 127; Integer c = 128; Integer d = 128; System.out.println(a == b); System.out.println(c == d); One prints true. One prints false. At first glance, both comparisons look identical. Same type. Same assignment. Same operator. So what’s going on? If you know the reason, you’ve got a deeper grasp of how Java actually works under the hood. Drop your explanation below — no Googling 👇 #Java #Programming #SoftwareEngineering #DevCommunity #CodeChallenge
To view or add a comment, sign in
-
Still confused about how Java actually runs your code? 🤔 Here’s a simple breakdown of how the JVM works 👇 👉 1. Build Phase ✔️ Java code (.java) is compiled using javac ✔️ Converted into bytecode (.class files) 👉 2. Class Loading ✔️ JVM loads classes using: Bootstrap Class Loader Platform Class Loader System Class Loader 👉 3. Linking ✔️ Verify → Prepare → Resolve ✔️ Ensures code is safe and ready to run 👉 4. Initialization ✔️ Static variables & blocks are initialized 👉 5. Runtime Data Areas ✔️ Method Area & Heap (shared) ✔️ Stack, PC Register, Native Stack (per thread) 👉 6. Execution Engine ✔️ Interpreter executes bytecode line by line ✔️ JIT Compiler converts hot code into machine code for speed 👉 7. Native Interface (JNI) ✔️ Interacts with native libraries when needed 💡 JVM is the reason behind Java’s “Write Once, Run Anywhere” power. 📌 Save this post 🔁 Repost to help others 👨💻 Follow Abhishek Sharma for more such content #Java #JVM #SystemDesign #BackendDevelopment #SoftwareEngineer #Developers #TechJobs #Programming #LearnJava
To view or add a comment, sign in
-
-
Topic of the day String? Why String is Immutable? 👉 In Java, a String is immutable, which means once it is created, its value cannot be changed. Example: String s = "Hello"; s.concat(" World"); 👉 You might expect: "Hello World" 👉 But actual output: "Hello" Because concat() creates a new object, instead of modifying the existing one. 🔍 Why did Java designers make String immutable? ✔️ Security – Strings are used in sensitive areas (like DB connections, file paths, network URLs) ✔️ Thread Safety – No need for synchronization (safe in multithreading) ✔️ Performance – Enables String Pool (memory optimization) ✔️ Caching – Hashcode can be cached (used in HashMap) If you are doing multiple string modifications, prefer: 👉 StringBuilder (faster, not thread-safe) 👉 StringBuffer (thread-safe) #Java #JavaConcepts #InterviewPreparation #Programming #Developers #Programming #Development #Coding
To view or add a comment, sign in
-
Checked vs Unchecked Exceptions - Know the Difference ⚠️ 🔹 Checked Exceptions ▸ Checked at compile time ▸ Must be handled (try-catch or throws) ▸ Not handled → code won’t compile ▸ Examples: IOException, SQLException, FileNotFoundException 🔹 Unchecked Exceptions ▸ Occur at runtime ▸ No need to handle (but recommended) ▸ Extend RuntimeException ▸ Examples: NullPointerException, ArrayIndexOutOfBoundsException 💡 Simple Way to Remember → Checked = Compiler forces handling → Unchecked = Runtime errors 🚀 Best Practice ▸ Use Checked → for recoverable scenarios (e.g., file not found → retry) ▸ Use Unchecked → for programming bugs (e.g., null → fix the code) #Java #SpringBoot #ExceptionHandling #JavaDeveloper #BackendDeveloper
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