1 2 3 4 5 6 7 8 9
All Flashcards
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
What is a String in Java?
An object representing a sequence of characters.
What is a String literal?
A sequence of characters enclosed in double quotes.
What is the String class?
A class in Java used to create and manipulate strings.
Define pre-initialized String.
A string created using double quotes, e.g., String str = "text";
Define String constructor.
Creating a string object using the new
keyword, e.g., String str = new String("text");