zuai-logo

What are the differences between == and .equals() when comparing objects in Java?

==: Compares object references (memory locations). .equals(): Compares object content (as defined by the class).

All Flashcards

What are the differences between `==` and `.equals()` when comparing objects in Java?
`==`: Compares object references (memory locations). `.equals()`: Compares object content (as defined by the class).
What are the differences between comparing primitive types and objects for equality in Java?
Primitives: Use `==` to compare values. Objects: Use `.equals()` to compare content (and `==` for reference equality).
What are the differences between using string literals and `new String()` to create `String` objects?
String literals: May be reused from the string pool. `new String()`: Always creates a new object in memory.
What are the differences between the default `.equals()` method (from `Object`) and an overridden `.equals()` method?
Default: Compares object references. Overridden: Compares object content based on custom logic.
What are the differences between checking for `null` before casting and after casting in an overridden `.equals()` method?
Before: Prevents `NullPointerException` during casting. After: May be necessary if the object's attributes can be null.
What are the differences between using `instanceof` and `getClass()` to check the type of an object in `.equals()`?
`instanceof`: Checks if an object is of a certain class or its subclass. `getClass()`: Checks if an object is exactly of a certain class.
What are the differences between using `==` to compare `Integer` objects and `int` primitives?
`Integer` objects: `==` compares references (may be true for small values due to caching). `int` primitives: `==` compares values.
What are the differences between using `equals()` for `String` objects and `Integer` objects?
`String` objects: `equals()` compares the character sequences. `Integer` objects: `equals()` compares the integer values.
What are the differences between overriding `equals()` and `hashCode()` methods?
`equals()`: Defines object equality. `hashCode()`: Generates an integer hash code for the object (should be consistent with `equals()`).
What are the differences between shallow copy and deep copy with respect to object equality?
Shallow copy: Copies references, so the original and copy share the same objects. Deep copy: Creates new objects for all referenced objects, ensuring independence.
What is the key difference between using `==` and `.equals()` for object comparison in Java?
`==` compares object references (memory locations), while `.equals()` compares object content.
Why is it generally recommended to use `.equals()` for comparing `String` objects in Java?
Because `==` only checks if the references are the same, while `.equals()` checks if the character sequences are the same.
When should you override the `.equals()` method in a custom class?
When you need to define a specific meaning of equality for objects of that class, based on their attributes.
What happens if you don't override the `.equals()` method in a custom class?
The `.equals()` method inherited from the `Object` class will be used, which only compares object references (like `==`).
How does the Java string pool affect the behavior of the `==` operator when comparing string literals?
String literals are often reused from the string pool, so `==` may return `true` even for distinct `String` objects with the same content.
Why does creating a `String` object with `new String()` behave differently than using a string literal?
`new String()` always creates a new object in memory, even if a string with the same content exists in the string pool.
What is the significance of the `@Override` annotation when overriding the `.equals()` method?
It indicates that you are intentionally overriding a method from a superclass, allowing the compiler to catch errors if the method signature is incorrect.
Explain the importance of checking for `null` in an overridden `.equals()` method.
To prevent a `NullPointerException` when comparing an object with a `null` reference.
Why is it important to check the class type in an overridden `.equals()` method?
To ensure that you are only comparing objects of the same class, as equality is typically only defined between objects of the same type.
What is the purpose of casting the `Object` parameter to the correct type in an overridden `.equals()` method?
To access the specific attributes of the object being compared, so you can perform the content comparison.
What is the definition of object equality?
Determining if two objects are considered the same, either by reference or content.
What is the definition of reference equality?
Two object references pointing to the exact same memory location.
What is the definition of content equality?
Two objects having the same attribute values, even if they are different objects in memory.
What is the definition of the `==` operator in Java?
An operator that compares the memory locations (references) of two objects.
What is the definition of the `.equals()` method in Java?
A method that compares the content of two objects, based on how the class defines equality.
What is meant by overriding the `.equals()` method?
Redefining the `.equals()` method in a subclass to provide a custom definition of object equality.
What is a string literal in Java?
A sequence of characters enclosed in double quotes, like "Hello".
What is the Java string pool?
A memory area in the Java Virtual Machine (JVM) that stores string literals to optimize memory usage.
What is a primitive type in Java?
Basic data types like `int`, `double`, `boolean`, etc., that are not objects.
What does it mean for an object to have identity?
An object's unique existence in memory, separate from other objects, even if they have the same content.