It's equivalent to declaring and initializing it at once:
String str = new String("Hello World");
You don't need to do new String("...")
though. You already have a String literal. You can just do:
String str = "Hello World";
Otherwise, you're taking the first, canonical String object ("Hello World"
) and unnecessarily using it to initialize a second object (new String(...)
) with the same text.
Edit: According to the choices you've posted now, it's not exactly equivalent to any of them. As explained in more detail by the other answers, "Hello World"
has a subtle (yet important) difference to new String("Hello World")
, so it is not exactly equivalent to str = "Hello World"
. On the other hand, the other three options don't compile at all, so that is certainly the intended answer.