zuai-logo

What does the following code output?

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

true

All Flashcards

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