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

What is the definition of object equality?

Determining if two objects are considered the same, either by reference or content.

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

All Flashcards

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.

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