Special Offer take any 4 courses for INR 21999.00*

Courses
0

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

December 26, 2024

In Java, == and .equals() are used to compare objects or variables, but they serve different purposes:
1. == Operator
Purpose: Compares references (memory addresses) of objects.
Usage: Used to check if two references point to the same object in memory.
Example:
String str1 = new String(“Hello”);
String str2 = new String(“Hello”);
System.out.println(str1 == str2); // false (different memory locations)
2. .equals() Method
Purpose: Compares the content (value) of objects.
Usage: Used to check if two objects have the same value or content.
Example:
String str1 = new String(“Hello”);
String str2 = new String(“Hello”);
System.out.println(str1.equals(str2)); // true (content is the same)

Leave a Comment

Drop a Query

Whether to upskill or for any other query, please drop us a line and we'll be happy to get back to you.

Drop a Query NEW

Request A Call Back

Please leave us your contact details and our team will call you back.

Request A Call Back

By tapping Submit, you agree to Cambridge infotech Privacy Policy and Terms & Conditions

Enquiry Now

Enquiry popup