1 2 3 4 5 6 7 8
Revise later
SpaceTo flip
If confident
All Flashcards
Why are strings in Java objects?
They belong to the String class, providing built-in methods for manipulation.
What is the difference between using `==` and `.equals()` to compare strings?
`==` compares references, `.equals()` compares content.
Explain string immutability.
Strings cannot be changed after creation; any modification creates a new String object.
Why are pre-initialized strings more efficient?
Java can reuse them, avoiding the creation of new objects for identical string literals.
What are the differences between creating a String with a literal and using the `new` keyword?
Literal: potentially reuses existing strings. `new`: always creates a new object.
What does the following code output?
String str1 = "hello";
String str2 = "hello";
System.out.println(str1 == str2);
true
What does the following code output?
String str1 = new String("hello");
String str2 = new String("hello");
System.out.println(str1 == str2);
false
What does the following code output?
String str1 = new String("hello");
String str2 = new String("hello");
System.out.println(str1.equals(str2));
true