Java String Comparison: == vs equals()

Day 10 – == vs .equals() in Java ⏳ 1 Minute Java Clarity – Understanding how Java compares Strings This is one of the most confusing topics for beginners in Java ❓ Are these the same? String a = "Java"; String b = "Java"; 👉 a == b → true 👉 a.equals(b) → true Looks same right? But wait ⚠️ 📌 What does == do? It checks if both references point to the same object (memory location). 📌 What does .equals() do? It checks if the values (content) are equal. 💥 Now see this: String a = new String("Java"); String b = new String("Java"); 👉 a == b → false ❌ (different objects in memory) 👉 a.equals(b) → true ✅ (same text content) 💡 Quick Summary ✔ == → compares memory addresses. ✔ .equals() → compares actual values. 🔹 Always use .equals() for Strings unless you specifically need to check if two variables point to the exact same memory slot. 🔹 Next → String Immutability in Java Have you ever spent hours debugging because of a == mistake? #Java #BackendDeveloper #JavaFullStack #LearningInPublic #Programming #JavaProgramming #equals() #SoftwareEngineering #TechCommunity 

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories