Raj Kumar
Computer Science And Engineering

What is the difference between == and .equals() in Java?

Java

Explanation

1161    0

Difference between == and .equals() in Java:

 

== is used to compare the memory addresses of objects, while .equals() compares the contents of objects.

 

Example: 

String str1 = new String("Hello");

String str2 = new String("Hello");

System.out.println(str1 == str2); // false (different memory addresses)

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



Share:   
   Raj Kumar
Computer Science And Engineering

More Questions from Java Module 0