Mostrando postagens com marcador ==. Mostrar todas as postagens
Mostrando postagens com marcador ==. Mostrar todas as postagens

Difference between == and .equals() in Java

In Java, == and .equals() are both used to compare things — but they compare different aspects of objects.


== (Reference Comparison)

== checks whether two references point to the exact same object in memory.

It does NOT check if the contents are equal.

Example:

String a = new String("hello");
String b = new String("hello");

System.out.println(a == b); // false

Even though both contain "hello", they are different objects in memory.

However:

String a = "hello";
String b = "hello";

System.out.println(a == b); // true

Because Java uses the String Pool, both references point to the same object.


.equals() (Content Comparison)

.equals() checks whether two objects are logically equal (same content).

For String, Integer, and many other classes, .equals() is overridden to compare values.

Example:

String a = new String("hello");
String b = new String("hello");

System.out.println(a.equals(b)); // true

Because the contents are the same.


Important Difference Summary

Feature==.equals()
ComparesMemory addressObject content
Works for primitives?✅ Yes❌ No
Works for objects?✅ Yes✅ Yes
Can be overridden?❌ No✅ Yes

Special Case: Primitives

For primitive types (int, double, boolean, etc.):

int x = 5;
int y = 5;

System.out.println(x == y); // true

== compares actual values.

.equals() cannot be used with primitives.


Important Warning (Null Safety)

String str = null;
str.equals("hello"); // ❌ NullPointerException

Safer way:

"hello".equals(str); // ✅ Safe

Real-World Rule of Thumb

  • Use == for primitives

  • Use .equals() for object value comparison

  • Use == for objects only when you want to check if they are the same instance



The difference between "==" and ".equals()" in Java

 In Java, "==" and ".equals()" are used to compare objects, but they have different purposes and behaviors:

"==" (Equality Operator)

The "==" operator checks referential equality, that is, it compares whether two objects reference exactly the same location in memory. This means that it checks whether both sides of the expression point to the same object. 

Example:

String str1 = new String("Hello");
String str2 = new String("Hello");
String str3 = str1;
System.out.println(str1 == str2); // false (different objects)
System.out.println(str1 == str3); // true (same reference)

".equals()"

The ".equals()" method is used to compare the contents of objects, not the memory reference. It is a method defined in the "Object" class and is often overridden by specific classes to provide a meaningful comparison implementation.

Example:

String str1 = new String("Hello");
String str2 = new String("Hello");

System.out.println(str1.equals(str2)); // true (same content)

In the above example, although "str1" and "str2" are different objects in memory, the ".equals()" method is overridden in the "String" class to compare the contents of the strings, resulting in "true".

Important Consideration

In many standard Java classes, such as "String", "equals()" is overridden to provide content comparison. However, in other classes, "equals()" may have the same behavior as "==" unless explicitly overridden.

Integer num1 = new Integer(5);
Integer num2 = new Integer(5);

System.out.println(num1 == num2);         // false (different references)
System.out.println(num1.equals(num2));    // true (same content)


In short, while "==" compares referential equality, ".equals()" compares the contents of objects. In many cases, especially when dealing with complex objects or user-defined classes, it is advisable to use ".equals()" to ensure correct content comparison.

HashMap in Java

Let’s break down how   HashMap   works internally (Java 8+ implementation). What Is a HashMap? HashMap<K, V>  is a  hash table–based  ...