🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
In Java, comparing objects and primitives is a fundamental operation. The == operator and the equals() method are two ways to compare objects, but they serve different purposes. The == operator compares references or primitive values, while the equals() method checks logical equality (content comparison).
2. Key Points
1. == checks if two references point to the same object or if two primitives have the same value.
2. equals() is a method in the Object class that checks if two objects are logically equal.
3. equals() can be overridden in a class to define custom equality logic.
4. Using == with objects often leads to unexpected results if you're looking for logical equality rather than reference identity.
3. Differences
| == Operator | equals() Method |
|---|---|
| The == operator is used for reference comparison. It checks if two object references point to the same object in memory. | The equals() method is used for content comparison. It checks if two objects are logically equal based on their content. |
| Has a single implementation in Java and cannot be overridden. | It can be overridden in any class to implement equality based on an object's state or data. |
| It works the same for primitive data types and objects. For primitives, == checks if the values are identical. | Primarily used with objects. Using it with primitive data types involves their wrapper classes and comparing object contents. |
| It performs faster comparisons because it compares memory addresses or primitive values. | It is potentially slower than == for objects because it checks the actual object content, which might involve multiple field comparisons. |
| The default behavior for objects cannot be changed. | The default behavior provided by Object class's equals() method can be overridden in user-defined classes for customized equality logic. |
4. Example
// Step 1: Create two string variables using the string literal
String example1 = "Hello";
String example2 = "Hello";
// Step 2: Create two string objects using the 'new' keyword
String object1 = new String("Hello");
String object2 = new String("Hello");
// Step 3: Use the == operator to compare the string variables and objects
System.out.println("Using == for literals: " + (example1 == example2)); // Compares string literals
System.out.println("Using == for objects: " + (object1 == object2)); // Compares object references
// Step 4: Use the equals() method to compare the string objects
System.out.println("Using equals for objects: " + object1.equals(object2)); // Compares string values
Output:
Using == for literals: true Using == for objects: false Using equals for objects: true
Explanation:
1. The string literals example1 and example2 refer to the same object in the string pool, so == returns true.
2. object1 and object2 are created with new, so they reference different objects, and == returns false.
3. The equals() method checks the content of object1 and object2, finds them identical, and returns true.
5. When to use?
- Use == to compare primitive values or when you need to check if two references point to the exact same object.
- Use equals() when you need to check if two objects are logically "equal", meaning they have the same content or state.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment