Skip to main content

Posts

Showing posts with the label Java Interview

πŸ’‘ Optional in Java 8 — NullPointerException's Worst Enemy (and Your Best Friend!)

πŸš€ Introduction Raise your hand if you've ever been attacked by this monster: java.lang.NullPointerException: Cannot invoke "getName()" because "user" is null We’ve all been there. You think your code is clean... But null is silently hiding, waiting to crash your app. πŸ’£ So, Java 8 gave us a gift: 🎁 Optional<T> — A smart wrapper to help us handle missing values without crashing the app. Let’s understand this with simple words, diagrams, examples, and yes — a few fun moments! πŸ˜„

🧡 Virtual Threads in Java — The Ultimate Guide with Diagrams, Code & Interview Qs!

πŸš€ “How are Virtual Threads different from Thread Pools?” 😡 “Are they OS threads or JVM threads?” πŸ™ƒ “Should I still use CompletableFuture?” 🀯 “How do I even use them in real-time microservices?” 🧠 What are Virtual Threads? Virtual Threads (introduced in Java 21 as stable πŸŽ‰) are lightweight threads managed by the JVM instead of the OS kernel. πŸ‘‰ They look like normal threads, but don’t hog OS resources like traditional threads. 🧠 What is the OS Kernel? πŸ›️ OS Kernel = The Brain of the Operating System It’s the core part of your OS (Windows, Linux, Mac) that: Manages memory 🧠 Schedules threads πŸ•’ Talks to hardware πŸ’» Handles I/O operations πŸ“¨ When you create a traditional thread in Java, the JVM asks the OS Kernel to create a real OS-level thread. πŸ–Ό️ Imagine This... ┌───────────────────────────┐ │ Your Java Application │ └────────────┬──────────────┘ │ ...

πŸ’₯ “Filter vs Interceptor in Spring — Who’s the Real Gatekeeper?”

πŸ’₯ “Filter vs Interceptor in Spring — Who’s the Real Gatekeeper?” πŸ” The Hidden Guards of Your REST APIs (with Real Examples, Jokes & Weird Discoveries!) 🎯 1) What Is Filter / Interceptor / OncePerRequestFilter? 🧹 Filter A Filter is a component from the Servlet API that sits at the very beginning of a request's journey. It is invoked before the Spring context even gets involved. It's great for tasks like CORS configuration, encoding setup, basic logging, and blocking or modifying raw requests/responses. 🎀 Think of Filter as the club bouncer who checks everyone at the entrance—before they even see the dance floor! πŸ•΅️ Interceptor A HandlerInterceptor is a Spring MVC feature that works after the request has passed through the DispatcherServlet but before the controller method is executed. It can inspect the handler (controller) method, add attributes, or block the request if needed. πŸ€“ It’s like the guy inside the club checking if you’re VIP before letting you in ...

🀯 “Why Integer a = 128; Integer b = 128; Broke My Brain”

🎬 Intro — “I Thought I Knew ==… Until Java Proved Me Wrong” I was casually writing: Integer a = 128; Integer b = 128; System.out.println(a == b); // Output: false ❌ …and confidently told myself: “Yeah, that’s definitely true.” πŸ’₯ WRONG! Output: false I was like… 🧍‍♂️ “Hello? Java, are you okay?” Let's break this down, and I’ll show you what I learned — in my usual style: with humor, diagrams, dumb-but-smart questions, and gotchas! πŸ§ͺ 1. Code Examples That Confused Me ✅ Primitive Comparison Works int a = 128; int b = 128; System.out.println(a == b); // true ✅ ➡️ Simple! Both values are 128, both stored in stack. Done. ❌ Object Comparison Fails? Integer a = 128; Integer b = 128; System.out.println(a == b); // false ❌ ➡️ Wait, same values, but still false? ✅ Mixed (Object + Primitive) Integer a = 128; int b = 128; System.out.println(a == b); // true ✅ ➡️ What? Now it works again?