zuai-logo
zuai-logo
  1. AP Computer Science A
FlashcardFlashcard
Study GuideStudy GuideQuestion BankQuestion BankGlossaryGlossary

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).

Flip to see [answer/question]
Flip to see [answer/question]
Revise later
SpaceTo flip
If confident

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 does the following code output?

java
String a = "test";
String b = "test";
System.out.println(a == b);

true

What does the following code output?

java
String a = new String("test");
String b = new String("test");
System.out.println(a == b);

false

What does the following code output?

java
String a = new String("test");
String b = new String("test");
System.out.println(a.equals(b));

true

What does the following code output?

java
String a = "test";
String b = new String("test");
System.out.println(a.equals(b));

true

What does the following code output?

java
Integer num1 = 1000;
Integer num2 = 1000;
System.out.println(num1 == num2);

false

What does the following code output?

java
Integer num1 = 100;
Integer num2 = 100;
System.out.println(num1 == num2);

true

What does the following code output?

java
Integer num1 = new Integer(100);
Integer num2 = new Integer(100);
System.out.println(num1 == num2);

false

What does the following code output?

java
Integer num1 = new Integer(100);
Integer num2 = new Integer(100);
System.out.println(num1.equals(num2));

true

What does the following code output?

java
String str1 = null;
String str2 = "hello";
System.out.println(str2.equals(str1));

false

Identify the error in the following code:

java
int num1 = 5;
Integer num2 = new Integer(5);
System.out.println(num1.equals(num2));

Cannot invoke .equals() on a primitive type (int). num1 should be an Integer object.

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.