Yuvraj Singh Kushwah’s Post

🌟 Day 7 of My Java Learning Journey ~> Understanding == vs .equals() in Java 🔥 Hey connections 👋 Today I explored one of the most commonly confusing topics in Java — the difference between == and .equals(). Let’s break it down simply 👇 💡 == Operator Used to compare memory references (i.e., whether two variables point to the same object in memory). Works perfectly for primitive data types like int, char, boolean, etc. But when used with objects (like String), it only checks if both references point to the same object, not if their values are equal. 🧩 Example: -------------------------------------code start---------------------------------------- String s1 = new String("Java"); String s2 = new String("Java"); System.out.println(s1 == s2); // false (different memory locations) System.out.println(s1.equals(s2)); // true (same value) --------------------------------------code end--------------------------------------- 💬 .equals() Method Comes from the Object class. Used to compare the actual content (values) of two objects. In most Java classes (like String, Integer, etc.), it’s overridden to check value equality. 🧩 Example: ----------------------------------code start------------------------------------------- String name1 = "Yuvi"; String name2 = "Yuvi"; System.out.println(name1 == name2); // true (same memory due to String pool) System.out.println(name1.equals(name2)); // true (same value) ----------------------------------code end------------------------------------------- 🚀 Quick Summary ~ Comparison TypeWorks OnChecks==Primitives ~ Object referencesMemory location.equals()ObjectsActual values/content ✨ Remember: Use == for primitive data types Use .equals() for comparing object values 💭 This small concept makes a big difference when working with strings, collections, and custom objects. #Java #Coding #LearningJourney #JavaDeveloper #OOPs #Programming #100DaysOfCode #DevOps #JavaLearning #SoftwareDevelopment #LinkedInLearning

  • text

To view or add a comment, sign in

Explore content categories