Skip to main content

Posts

Showing posts with the label Java Interview Questions

πŸ₯‘πŸ₯’JSON vs HashMap — Why They’re Cousins, Not Twins! πŸ₯‘πŸ₯’

πŸ— Structure 1️⃣ Setting the Scene — The Confusion Moment πŸ€” "I thought JSON was just a HashMap in a superhero costume!!" Nope! It’s like saying Spring Boot is just Spring MVC with coffee ☕ — there’s way more going on. 2️⃣ πŸ’‘ What is a HashMap? Java-only data structure Stores key-value pairs in memory (heap) Keys can be anything — Integer, String, custom objects (if hashCode() & equals() are implemented properly) Not meant for network transfer πŸ“¦ Example: Map<Integer, String> studentMap = new HashMap<>(); studentMap.put(101, "Anand"); studentMap.put(102, "Priya"); This stays inside your JVM. If you try to send it over HTTP as-is… good luck explaining it to a Python service. 🐍πŸ’₯ 3️⃣ πŸ“ What is JSON? Language-independent data format (Java, Python, JS, Go, you name it) Stores only String...

πŸ›’ Spring Bean Scopes – Explained with an E-Commerce Website

☕ Spring Bean Scopes – Explained Like You’re Shopping Online πŸ›’ In one of my interviews, I was asked a simple question: "What are the scopes in Spring and when will you use them?" I thought I knew it... until the interviewer confused me so much that I started mixing prototype with singleton and session with application 🀯. So here’s my real-time, e-commerce-style explanation of Spring bean scopes — with code, diagrams, and some fun analogies! 1️⃣ Singleton Scope – πŸ“¦ The Universal Product Definition: Spring creates only one instance of the bean for the entire application context. Every @Autowired injection gets the same object . πŸ›’ E-Commerce Example: Think of Amazon's Shipping Service . Every order uses the same logic, the same configuration — no need to create a new object for each request. @Component @Scope("singleton") // Default one , So you don't want to mention explicitly public class ShippingService { private String shipp...

🚚 Pass By Value vs Pass By Reference in Java — The Great Confusion Buster πŸ’£

  πŸ’¬ “I changed the object inside a method. But when I came back… NOTHING happened. Java, are you even listening to me?!” 😑 That was me. Screaming at my screen. So I decided to dig deep: What’s really going on? Let’s go from basics → to bugs → to JVM internals → to truth πŸ’‘ 🎯 What People Think Java Does: public void change ( int x) { x = 10 ; } “Cool, this is pass-by-value. Just a copy. I get it.” ✅ But then... public void update (User u) { u.setName( "Anand" ); } “Wait, it changed outside! That must be pass-by-reference!” ❓ And then... public void update (User u) { u = new User ( "NewGuy" ); } “Now it DIDN’T change outside?!  Java, are you drunk?” 🍺 🧠 Truth Bomb: Java is Always Pass-by-Value. No Exceptions. Even for objects. But here's the tricky twist: πŸ“Œ Java passes a copy of the reference when dealing with objects. So you're not passing the object directly — You're passing a copy of the point...

πŸ₯Š @Component vs @Configuration — Not Just Cousins in Spring! 😲

 πŸ§  Ever looked at these two annotations and thought: “Aren’t they doing the same thing ?” Welcome to the club. I had the same confusion until... I went deep down the Spring rabbit hole πŸ•³️πŸ‡ Let’s clear this once and for all — with real differences , examples , gotchas , and even a few bad jokes along the way 😎 🌟 The Basic Definitions Annotation Meaning @Component A general-purpose stereotype indicating a class is a Spring-managed bean @Configuration A specialized class used to declare beans via @Bean methods πŸ’‘ Both register beans with Spring — but what happens behind the scenes is where it gets spicy 🌢️ πŸ€” But Aren’t They Both Just “Beans”? Short answer: Yes . Long answer: It’s complicated — like your last relationship πŸ’”. ⚙️ INTERNAL MAGIC: The Real Difference 1. @Con...