Examveda
Examveda

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


This Question Belongs to Java Program >> Strings

Join The Discussion

Comments ( 2 )

  1. Vamsi Vamsi
    Vamsi Vamsi :
    5 months ago

    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.

  2. Science And
    Science And :
    6 months ago

    how is this possible

Related Questions on Strings