Understanding == vs .equals() in Java: A Comprehensive Guide

Understanding == vs .equals() in Java: A Comprehensive Guide

In Java, comparing objects might seem straightforward, but it often leads to confusion due to the different methods available. Two of the most common methods for comparison are == and .equals(). Understanding the difference between these two can help you avoid bugs and write more effective Java code.

The == Operator

The == operator is used for reference comparison, meaning it checks whether two references point to the same object in memory. It does not compare the contents of the objects, only their memory addresses.

Examples:

String s1 = "hello world";
String s2 = "hello world";
System.out.println(s1==s2) // true        

The above example returns true because the == operators checks if two references point to the same object. or not

Below how its looks Pictorially


The Eclipse Implementation ! I wanted to dive into check the memory adress of a variable visually so i used System.identityHashCode(Object obj) // this method return unique Integer values



hascode() method and intHashcode are not same and hascode is not the adress of a variable

The .equals() Method

.equals() method, on the other hand, is intended for content comparison. It is defined in the Object class and is meant to be overridden by subclasses to provide meaningful equality checks.

String str1 = new String("Hello");
String str2 = new String("Hello");
System.out.println(str1.equals(str2)); // Output: true        

This is how pictorial representation of how objects will be created with new keyword


observe the following outputs whats happening below


In Java, the equals method in the Object class checks for reference equality, returning true only if both references point to the same object. In contrast, the String class overrides equals to first check for reference equality and, if they are different, then compare the content of the strings. The System.identityHashCode() method provides a hash code based on the object's memory address, which helps identify objects but does not directly compare their contents.

public boolean equals(Object obj) {
    return (this == obj);
}
// default equals method in Object Class

public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String aString = (String) anObject;
        return this.value.equals(aString.value);
    }
    return false;
}
// overidden .equal method in String class        
narahari reddy

java8|| DSA||Lld||systemdesign || springboot|| microservices || kafka|| upskilling atscaler

3 个月

Love this

Jagan Baskar

Software Engineer ? LinkedIn Top Problem Solving Voice '24 ? Open-source Contributor ? Core Java ? Data Structures and Algorithms ? MySQL ? Spring Boot ? Design Patterns ? Low-Level Design.

3 个月

Useful tips Naveen Reddy ????

要查看或添加评论,请登录

Naveen Reddy的更多文章

  • Understanding Spring Core: Dependency Injection

    Understanding Spring Core: Dependency Injection

    Introduction Spring Core is the foundation of the Spring Framework, and one of its most powerful features is Dependency…

    3 条评论

社区洞察

其他会员也浏览了