🚨 𝗪𝗵𝘆 𝗼𝘃𝗲𝗿𝗿𝗶𝗱𝗶𝗻𝗴 𝗲𝗾𝘂𝗮𝗹𝘀() 𝘄𝗶𝘁𝗵𝗼𝘂𝘁 𝗵𝗮𝘀𝗵𝗖𝗼𝗱𝗲() 𝗯𝗿𝗲𝗮𝗸𝘀 𝗽𝗿𝗼𝗱𝘂𝗰𝘁𝗶𝗼𝗻 This is one of those Java rules that feels academic… until it breaks your system at scale. 𝗧𝗵𝗲 𝗿𝘂𝗹𝗲 𝗶𝘀 𝘀𝗶𝗺𝗽𝗹𝗲: 𝗜𝗳 𝘁𝘄𝗼 𝗼𝗯𝗷𝗲𝗰𝘁𝘀 𝗮𝗿𝗲 𝗲𝗾𝘂𝗮𝗹 𝗮𝗰𝗰𝗼𝗿𝗱𝗶𝗻𝗴 𝘁𝗼 𝗲𝗾𝘂𝗮𝗹𝘀(), 𝘁𝗵𝗲𝘆 𝗠𝗨𝗦𝗧 𝗿𝗲𝘁𝘂𝗿𝗻 𝘁𝗵𝗲 𝘀𝗮𝗺𝗲 𝗵𝗮𝘀𝗵𝗖𝗼𝗱𝗲(). Ignoring this doesn’t fail compilation. It fails 𝘀𝗶𝗹𝗲𝗻𝘁𝗹𝘆 — and that’s what makes it dangerous. 💥 𝗪𝗵𝗮𝘁 𝗮𝗰𝘁𝘂𝗮𝗹𝗹𝘆 𝗯𝗿𝗲𝗮𝗸𝘀 𝗶𝗻 𝗽𝗿𝗼𝗱𝘂𝗰𝘁𝗶𝗼𝗻? 🔹 𝗛𝗮𝘀𝗵-𝗯𝗮𝘀𝗲𝗱 𝗰𝗼𝗹𝗹𝗲𝗰𝘁𝗶𝗼𝗻𝘀 𝗺𝗶𝘀𝗯𝗲𝗵𝗮𝘃𝗲 Classes like: • HashMap • HashSet • ConcurrentHashMap 𝗿𝗲𝗹𝘆 𝗼𝗻 𝗯𝗼𝘁𝗵 hashCode() and equals(). If you override only equals(): • Duplicate objects appear in HashSet • HashMap.get() suddenly returns null • Cache lookups fail randomly • Memory usage grows unexpectedly 🔹 𝗘𝘅𝗮𝗺𝗽𝗹𝗲 𝗼𝗳 𝘁𝗵𝗲 𝗵𝗶𝗱𝗱𝗲𝗻 𝗯𝘂𝗴 You insert an object into a HashSet. Later, you check if it exists — and it doesn’t. Why? • equals() says they are equal • hashCode() sends them to 𝗱𝗶𝗳𝗳𝗲𝗿𝗲𝗻𝘁 𝗯𝘂𝗰𝗸𝗲𝘁𝘀 Result: 👉 The object exists… but can’t be found. 🔹 𝗪𝗵𝘆 𝘁𝗵𝗶𝘀 𝗶𝘀 𝘄𝗼𝗿𝘀𝗲 𝗶𝗻 𝗿𝗲𝗮𝗹 𝘀𝘆𝘀𝘁𝗲𝗺𝘀 • Caching layers break • Deduplication fails • Security checks behave inconsistently • Bugs appear 𝗼𝗻𝗹𝘆 𝘂𝗻𝗱𝗲𝗿 𝗹𝗼𝗮𝗱 • Debugging becomes a nightmare These issues are 𝗻𝗼𝗻-𝗱𝗲𝘁𝗲𝗿𝗺𝗶𝗻𝗶𝘀𝘁𝗶𝗰 — the worst kind. ✅ 𝗧𝗵𝗲 𝗰𝗼𝗿𝗿𝗲𝗰𝘁 𝗮𝗽𝗽𝗿𝗼𝗮𝗰𝗵 Whenever you override equals(): ✔ Always override hashCode() ✔ Use the same fields in both ✔ Keep them immutable if possible ✔ Use IDE or Lombok (@EqualsAndHashCode) carefully 🎯 𝗕𝗮𝗰𝗸𝗲𝗻𝗱 𝗿𝗲𝗮𝗹𝗶𝘁𝘆 𝗰𝗵𝗲𝗰𝗸 Most production bugs aren’t caused by complex logic. They’re caused by 𝗯𝗿𝗼𝗸𝗲𝗻 𝗰𝗼𝗻𝘁𝗿𝗮𝗰𝘁𝘀. And equals() / hashCode() is one of the most important contracts in Java. #Java #BackendDevelopment #SoftwareEngineering #JavaInternals #CleanCode #SystemDesign #InterviewPrep #SpringBoot #ProductionBugs
Java equals() and hashCode() Best Practices for Production Code
More Relevant Posts
-
🔍 How Java HashMaps Really Work — A Clear Visual Breakdown Most developers use HashMap every day, but very few understand what actually happens underneath. This visual explains the internal working in a simple way: ✔ Every key goes through a hash function ✔ The hash decides the bucket index (hash % capacity) ✔ Buckets store entries as LinkedLists ✔ Java 8+ converts LinkedLists → Red-Black Trees when collisions grow (threshold = 8) ✔ This improves lookup from O(n) → O(log n) It’s a great example of how something that looks simple is backed by very smart engineering. 🔍 Additional HashMap Internals (Beyond the Image) For those who want to go deeper: 🔸 Load Factor (0.75 default) – When 75% full, the HashMap resizes 🔸 Resizing & Rehashing – Creates a new array and redistributes entries 🔸 Null Key Allowed – Stored in bucket 0 🔸 Null Values Allowed 🔸 Hash Spreading – Improves key distribution to reduce collisions 🔸 Treeification Threshold = 8 – List → Tree conversion 🔸 Untreeification Threshold = 6 – Tree → List conversion 🔸 Not Thread-Safe → Use ConcurrentHashMap for concurrency 💡 Why this matters Understanding HashMap internals helps you: ✔ Write more efficient code ✔ Avoid unnecessary collisions ✔ Predict resizing and memory behavior ✔ Handle concurrency safely ✔ Ace Java interviews & system design discussions Small things like load factor, hashing, and treeification make a massive impact at scale. #Java #HashMap #Programming #JavaDeveloper #SystemDesign #DSA #Backend #Coding #TechEducation #Developers #Learning
To view or add a comment, sign in
-
-
☕ 𝗛𝗼𝘄 𝗝𝗮𝘃𝗮 𝗖𝗼𝗺𝗽𝗶𝗹𝗲𝘀 𝗦𝗼𝘂𝗿𝗰𝗲 𝗖𝗼𝗱𝗲 𝘁𝗼 𝗕𝘆𝘁𝗲𝗰𝗼𝗱𝗲 Ever wondered what happens after you write Java code? Let’s break it down step by step 👇 🧑💻 𝗔𝘁 𝗖𝗼𝗺𝗽𝗶𝗹𝗲 𝗧𝗶𝗺𝗲 1️⃣ 𝗪𝗿𝗶𝘁𝗲 𝗖𝗼𝗱𝗲 You write Java code in a .java file using classes, methods, and objects. 2️⃣ 𝗟𝗲𝘅𝗶𝗰𝗮𝗹 𝗔𝗻𝗮𝗹𝘆𝘀𝗶𝘀 The compiler (javac) scans the source and converts it into tokens ➡️ keywords, identifiers, literals, symbols. 3️⃣ 𝗦𝘆𝗻𝘁𝗮𝘅 𝗔𝗻𝗮𝗹𝘆𝘀𝗶𝘀 Checks if the code follows Java grammar rules and builds a Parse Tree 🌳 4️⃣ 𝗦𝗲𝗺𝗮𝗻𝘁𝗶𝗰 𝗔𝗻𝗮𝗹𝘆𝘀𝗶𝘀 Validates data types, variable declarations, and rule correctness ➡️ catches type mismatches and invalid references. 5️⃣ 𝗕𝘆𝘁𝗲𝗰𝗼𝗱𝗲 𝗚𝗲𝗻𝗲𝗿𝗮𝘁𝗶𝗼𝗻 Generates platform-independent bytecode stored in a .class file. 6️⃣ 𝗢𝗽𝘁𝗶𝗺𝗶𝘇𝗮𝘁𝗶𝗼𝗻 Applies basic optimizations to improve execution efficiency ⚡ ⚙️ 𝗔𝘁 𝗥𝘂𝗻𝘁𝗶𝗺𝗲 (𝗜𝗻𝘀𝗶𝗱𝗲 𝘁𝗵𝗲 𝗝𝗩𝗠) 7️⃣ 𝗖𝗹𝗮𝘀𝘀 𝗟𝗼𝗮𝗱𝗲𝗿 Loads .class files into memory. 8️⃣ 𝗕𝘆𝘁𝗲𝗰𝗼𝗱𝗲 𝗩𝗲𝗿𝗶𝗳𝗶𝗲𝗿 Ensures safety and prevents illegal or malicious operations 🔒 9️⃣ 𝗜𝗻𝘁𝗲𝗿𝗽𝗿𝗲𝘁𝗲𝗿 / 𝗝𝗜𝗧 𝗖𝗼𝗺𝗽𝗶𝗹𝗲𝗿 Converts bytecode into native machine code ➡️ JIT boosts performance by compiling hot code paths 🚀 ✅ 𝗧𝗵𝗲 𝗥𝗲𝘀𝘂𝗹𝘁 ✔️ Platform independence ✔️ Secure execution ✔️ Automatic memory management ✔️ Runtime performance optimization 𝗪𝗿𝗶𝘁𝗲 𝗼𝗻𝗰𝗲, 𝗿𝘂𝗻 𝗮𝗻𝘆𝘄𝗵𝗲𝗿𝗲 isn’t magic — it’s the JVM at work ☕💡 Which part of the Java compilation process did you first learn about? 👇 #Java #JVM #Bytecode #JavaInternals #SoftwareEngineering #BackendDevelopment
To view or add a comment, sign in
-
-
I just published a short write-up on async-bulkhead v0.3.0, a small Java library I’ve been working on around explicit overload control in async systems. The core idea is intentionally narrow: bound the number of in-flight async operations and fail fast when saturated. No queues, no retries, no execution model. v0.3.0 tightens cancellation semantics, adds non-intrusive observability hooks, and makes the core invariant explicit and test-backed. If you’ve dealt with async overload, fan-out amplification, or unclear “in-flight” semantics, this may resonate. Medium article: https://lnkd.in/gY5Chp_x Repo & docs: https://lnkd.in/gbwYeWyP Feedback (especially critical) is welcome.
To view or add a comment, sign in
-
Lately, I’ve been revisiting the Java Streams API, and it still amazes me how much cleaner and more expressive it makes our code. Streams shift our mindset from “How do I loop through this?” to “What transformation do I want?” Here’s a simple example that shows the power of Streams: List<String> names = Arrays.asList("John", "Anna", "Mark", "Sara", "Tom"); List<String> filteredNames = names.stream() .filter(name -> name.startsWith("A") || name.startsWith("S")) .map(String::toUpperCase) .sorted() .collect(Collectors.toList()); System.out.println(filteredNames); 🔍 What this does: * Filters names starting with A or S * Converts them to uppercase * Sorts them alphabetically * Collects them into a new list All in a clean, readable pipeline. Still learning. Still improving. Java keeps evolving, and so do we. #Java #StreamsAPI #CleanCode #SoftwareEngineering #BackendDevelopment #CodingTips
To view or add a comment, sign in
-
𝐎𝐛𝐣𝐞𝐜𝐭 𝐂𝐥𝐚𝐬𝐬 𝐌𝐞𝐭𝐡𝐨𝐝𝐬 𝐄𝐯𝐞𝐫𝐲 𝐉𝐚𝐯𝐚 𝐃𝐞𝐯𝐞𝐥𝐨𝐩𝐞𝐫 𝐒𝐡𝐨𝐮𝐥𝐝 𝐊𝐧𝐨𝐰 In Java, everything ultimately extends the Object class. Understanding its core methods is crucial for writing clean, predictable, and interview-ready code. Here’s a quick guide: 1. toString() – Returns a string representation of the object. Always override it for meaningful logging & debugging. 2. equals(Object obj) – Compares object content, not memory. Always override along with hashCode(). 3. hashCode() – Generates an integer hash for the object. Essential for collections like HashMap, HashSet. Must be consistent with equals(). 4. clone() – Creates a shallow copy of the object. Rarely used in production, but important for interviews. 5. getClass() – Returns runtime class information. Useful for reflection, debugging, and frameworks. 6. wait(), notify(), notifyAll() – Core of Java’s thread communication. Only works inside synchronized blocks. Still relevant for concurrency interviews. 7. finalize() (Deprecated) – Called by the GC before destroying an object. Avoid using it; prefer try-with-resources or cleaners. Interview Tip: Be ready to explain why & when you’d override these methods and their impact on collections, threading, and object behavior. #Java #Java25 #CleanCode #Programming #BackendDeveloper #TechUpdates #Java #CoreJava #JavaDeveloper #SpringBoot #BackendDevelopment
To view or add a comment, sign in
-
-
This Java behavior looks harmless — until it breaks your code 👇 Two objects. Same data. Still… not equal. When you override `equals()` but forget `hashCode()`, collections like HashMap quietly stop behaving correctly. Why? Hash-based collections rely on: 1️⃣ hashCode() to find the bucket 2️⃣ equals() to confirm equality If two objects are “equal” but produce different hash codes, they land in different buckets — and Java treats them as unrelated. Result? • Duplicate keys • Missing lookups • Very confusing bugs That’s why Java’s contract is strict: 👉 If equals() is overridden, hashCode() MUST be overridden too. Small rule. Big impact. Have you ever debugged a HashMap issue like this? #Java #BackendEngineering #JavaInterview #CleanCode
To view or add a comment, sign in
-
𝗧𝗵𝗿𝗲𝗮𝗱-𝘀𝗮𝗳𝗲𝘁𝘆 𝗶𝘀 𝗻𝗼𝘁 𝗮𝗯𝗼𝘂𝘁 𝘀𝘆𝗻𝗰𝗵𝗿𝗼𝗻𝗶𝘇𝗲𝗱, 𝗶𝘁’𝘀 𝗮𝗯𝗼𝘂𝘁 𝗼𝘄𝗻𝗲𝗿𝘀𝗵𝗶𝗽 When I first started writing multithreaded Java code, I saw this everywhere: • synchronized slapped on random methods • shared ArrayList being updated from multiple threads • quick fixes like volatile without understanding why • one bug disappears… and a new race condition appears somewhere else Did it work? Sometimes. Was it clean, scalable, and maintainable? No! That’s when I really understood what thread-safety actually means in real systems. 𝗧𝗵𝗲 𝗸𝗲𝘆 𝗶𝗻𝘀𝗶𝗴𝗵𝘁: Thread-safety is not a keyword. It’s a design decision about who owns the data and who is allowed to mutate it. Instead of trying to protect everything, good Java systems do the opposite: • reduce shared mutable state • prefer immutability (make state unchangeable by default) • confine mutation to one place (one thread / one component) • use the right concurrency tools only at boundaries (executors, concurrent collections, locks) 𝗧𝗵𝗲 𝗿𝗲𝘀𝘂𝗹𝘁: • fewer race conditions and “random” production bugs • simpler debugging (because the mutation points are predictable) • better performance than over-synchronizing everything • code that stays stable even when load increases Instead of each class deciding how to be thread-safe, the application clearly states: When state is shared, it has a single owner. When state changes, it happens in one controlled place. Always. 𝗟𝗲𝘀𝘀𝗼𝗻 𝗹𝗲𝗮𝗿𝗻𝗲𝗱 Concurrency is not about writing more locks. It’s about clear responsibility boundaries for data. And often, growing as a Java developer is less about learning new tricks and more about unlearning the habit of sharing state everywhere. 😆 #Java #Concurrency #ThreadSafety #Multithreading #Immutability #CleanCode #SoftwareArchitecture #BackendEngineering #DistributedSystems #ScalableSystems #BestPractices #EngineeringCulture
To view or add a comment, sign in
-
🚨 STOP SCROLLING — This ONE line exposes most Java myths User u = new User(); If your explanation ends at 👉 “object in heap, reference in stack” then you do NOT understand Java memory yet. And that’s okay — most people don’t. Java Memory Model & Object Lifecycle This is where GC, concurrency, performance, and memory leaks actually come from. This topic looks simple. It is not. And the confusion only shows up in production. What REALLY happens inside the JVM (no hand-waving) 1️⃣ Class loading (happens once) Before any object exists: User.class is loaded into Metaspace Bytecode is verified Runtime metadata is created 👉 This does not happen per object. 2️⃣ Heap memory allocation JVM allocates memory for: Instance fields Object header Mark Word (GC, locking, hash) Klass Pointer 📍 Object starts life in Young Generation 3️⃣ Zero initialization (mandatory) Before your constructor runs: int = 0 boolean = false reference = null 👉 Java guarantees safe defaults. 4️⃣ Constructor execution Now — and only now: User() runs Fields get real values 5️⃣ Reference assignment u lives on the stack Stack stores address, not the object 🔥 THE PART MOST PEOPLE MISS When u goes out of scope: ❌ Object is NOT deleted ❌ GC does NOT run immediately ✅ Only the stack entry disappears ✅ Heap object survives until unreachable from GC Roots This single fact explains: Memory leaks GC confusion “Why is this object still alive?” bugs Why you should care If you don’t understand this: GC feels random Memory leaks feel mysterious Concurrency bugs feel “weird” Performance tuning becomes guessing If you do: GC behavior becomes predictable Memory leaks become obvious Concurrency rules make sense JVM stops being a black box JVM internals are the engine. Java Memory Model is the physics. Learn the physics once — and GC, volatile, synchronization, and performance suddenly click. #Java #JavaMemoryModel #JVM #GarbageCollection #Concurrency #BackendEngineering #PerformanceEngineering #LowLevelDesign #SystemDesign #JavaDeveloper
To view or add a comment, sign in
-
𝗪𝗵𝗮𝘁 "𝗩" (𝗪𝗘) 𝗟𝗲𝗮𝗿𝗻 𝗘𝗽𝗶𝘀𝗼𝗱𝗲 𝟮 𝗛𝗼𝘄 𝗝𝗮𝘃𝗮 𝗪𝗼𝗿𝗸𝘀 ? 𝗬𝗲𝘀, 𝘆𝗼𝘂 𝗵𝗮𝘃𝗲 𝘄𝗿𝗼𝘁𝗲 𝗮 𝗝𝗮𝘃𝗮 𝗖𝗼𝗱𝗲 𝘀𝗲𝘃𝗲𝗿𝗮𝗹 𝘁𝗶𝗺𝗲 𝗵𝗮𝘃𝗲 𝘆𝗼𝘂 𝗲𝘃𝗲𝗿 𝘁𝗵𝗶𝗻𝗸... • how it works ? • why, need to compile it ? • how it work on any device ? • why, it not run without main function ? • What makes java one of the top language ? These are the some Questions, which come in my mind when i started my Java Journey.... Lets figure out , how it work... 1. Write Java source code (.𝚓𝚊𝚟𝚊) 2. Compile using 𝚓𝚊𝚟𝚊𝚌 → creates bytecode (.𝚌𝚕𝚊𝚜𝚜) 3. Class Loader loads .𝚌𝚕𝚊𝚜𝚜 file into memory 4. Bytecode Verifier checks security & validity 5. JVM interprets or JIT-compiles bytecode 6. Machine code is executed 7. Output is produced Here are 𝗶𝗺𝗽𝗼𝗿𝘁𝗮𝗻𝘁 𝗝𝗮𝘃𝗮 𝗶𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗤&𝗔 𝗤𝟭. 𝗪𝗵𝘆 𝗝𝗮𝘃𝗮 𝗶𝘀 𝗽𝗹𝗮𝘁𝗳𝗼𝗿𝗺-𝗶𝗻𝗱𝗲𝗽𝗲𝗻𝗱𝗲𝗻𝘁? A. Because Java compiles into 𝗯𝘆𝘁𝗲𝗰𝗼𝗱𝗲, which runs on any 𝗝𝗩𝗠. 𝗤𝟮. 𝗪𝗵𝗮𝘁 𝗵𝗮𝗽𝗽𝗲𝗻𝘀 𝗮𝗳𝘁𝗲𝗿 𝘄𝗿𝗶𝘁𝗶𝗻𝗴 𝗝𝗮𝘃𝗮 𝗰𝗼𝗱𝗲? A. Code → Compile → Bytecode → JVM → Execution → Output. 𝗤𝟯. 𝗪𝗵𝗮𝘁 𝗶𝘀 𝗯𝘆𝘁𝗲𝗰𝗼𝗱𝗲? A. Intermediate code generated by the Java compiler. 𝗤𝟰. 𝗪𝗵𝗮𝘁 𝗶𝘀 𝗝𝗩𝗠? A. JVM executes bytecode and converts it into machine code. 𝗤𝟱. 𝗗𝗶𝗳𝗳𝗲𝗿𝗲𝗻𝗰𝗲 𝗯𝗲𝘁𝘄𝗲𝗲𝗻 𝗝𝗗𝗞 𝗮𝗻𝗱 𝗝𝗥𝗘? A. JDK = Develop + Run, JRE = Run only. 𝗤𝟲. 𝗪𝗵𝗮𝘁 𝗶𝘀 𝗝𝗜𝗧 𝗰𝗼𝗺𝗽𝗶𝗹𝗲𝗿? A. Converts frequently used bytecode into machine code for speed. 𝗤𝟳. 𝗜𝘀 𝗝𝗮𝘃𝗮 𝗰𝗼𝗺𝗽𝗶𝗹𝗲𝗱 𝗼𝗿 𝗶𝗻𝘁𝗲𝗿𝗽𝗿𝗲𝘁𝗲𝗱? A. Both — compiled to bytecode, interpreted by JVM. 𝗤𝟴. 𝗪𝗵𝗮𝘁 𝗶𝘀 𝗖𝗹𝗮𝘀𝘀 𝗟𝗼𝗮𝗱𝗲𝗿? A. Loads .𝚌𝚕𝚊𝚜𝚜 files into JVM memory. 𝗤𝟵. 𝗪𝗵𝘆 𝗯𝘆𝘁𝗲𝗰𝗼𝗱𝗲 𝘃𝗲𝗿𝗶𝗳𝗶𝗰𝗮𝘁𝗶𝗼𝗻 𝗶𝘀 𝗻𝗲𝗲𝗱𝗲𝗱? A. For security and code safety. 𝗤𝟭𝟬. 𝗖𝗮𝗻 𝘄𝗲 𝗿𝘂𝗻 𝗝𝗮𝘃𝗮 𝘄𝗶𝘁𝗵𝗼𝘂𝘁 𝗝𝗩𝗠? A. No, JVM is mandatory. #Java #JavaDeveloper #SoftwareEngineering #ComputerScience #LearningInPublic #CareerGrowth #TechCommunity #ProfessionalDevelopment #DhoomBoy #Krish
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