Skip to main content

Posts

Showing posts with the label Java Basics

πŸ₯‘πŸ₯’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...

🚚 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...