Arepalli Chandra kanth’s Post

💡 Java Comparison: == vs .equals() In Java, both == and .equals() are used for comparison - but they work very differently == Operator (Equality Operator) : The == operator is used for identity comparison or value comparison depending on the variable type: Primitive Types (e.g., int, boolean, char): Compares the actual values stored in the variables. Example: 5 == 5 is true. Object Reference Types (e.g., String, custom classes): Compares the memory addresses (references) of the objects. It returns true only if both variables point to the exact same object instance in memory. It does not check if the objects have the same content or state. >Compares memory addresses (references), not actual content. >Returns true only if both references point to the same object. >Used mainly for primitive types or object identity checks. .equals() Method : The .equals() method is used for content comparison or value equality for objects: Object Reference Types: Compares the contents or state of the two objects to determine if they are logically equal. This is a method inherited from the base Object class. Default Behavior: By default, in the Object class, equals() performs the same reference comparison as the == operator. Overridden Behavior: Classes like String, Integer, and other wrapper classes override the equals() method to implement a meaningful check for content equality >Compares the actual content (values) of the objects. >The String class overrides .equals() to compare character sequences. >Returns true if contents are identical, even if references differ. In Short : 🔹 == → checks if both objects are the same in memory 🔹 .equals() → checks if both objects have the same value Special thanks to my mentors Anand Kumar Buddarapufor guiding me to clearly understand how Java handles object comparison and memory management. #Java #String #Equals #ProgrammingConcepts #Codegnana #LearningInPublic #Mentorship

  • diagram

To view or add a comment, sign in

Explore content categories