What is the correct way to create a new empty String object in Java?
A. String emptyString = "";
B. String emptyString = new String();
C. String emptyString = " ";
D. String emptyString = null;
E. Both A and B
Answer: Option E
Join The Discussion
Comments ( 2 )
Related Questions on Strings
In Java, which class is used to represent a sequence of characters as a string?
A. String
B. StringBuilder
C. StringSequence
D. StringArray
What is the correct way to create a new empty String object in Java?
A. String emptyString = "";
B. String emptyString = new String();
C. String emptyString = " ";
D. String emptyString = null;
E. Both A and B
Which of the following methods is used to compare two strings for equality in Java?
A. equals()
B. compareTo()
C. equalsIgnoreCase()
D. compare()
E. Both A and B
Both option A (`String emptyString = "";`) and option B (`String emptyString = new String();`) are correct ways to create a new empty `String` object in Java.
Option A uses a string literal to create an empty string, and option B uses the `new` keyword along with the `String` constructor to create an empty string. Both approaches are valid, but using the empty string literal (option A) is more common and generally preferred for simplicity and efficiency.
how is this possible