Java String Comparison: == vs equals()

📘 Day 12 – Java String | == vs equals() Today while learning Java, I understood an important concept about String comparison — the difference between == and equals(). What I learned 👇 🔹 String in Java String is a class String is immutable (once created, it cannot be changed) 🔹 == operator Checks memory reference Used to check whether both variables point to the same object String s1 = "Java"; String s2 = "Java"; System.out.println(s1 == s2); // true 🔹equals() method Checks the actual value/content of the string String s3 = new String("Java"); String s4 = new String("Java"); System.out.println(s3.equals(s4)); // true System.out.println(s3 == s4); // false 🔹 Difference == → compares memory equals() → compares value ✅ Conclusion: This concept is very important for Java interviews and for writing correct code. Learning basics like this helps me build strong Java fundamentals. #Day12 #Java #String #CoreJava #JavaBasics #JavaInterview #LearningJourney #PlacementPreparation

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories