In Java, is String s = new String () any use at all?

Since strings are immutable in Java, why do I want to use the String constructor with no arguments and create an object?

How the variable sis useful to me after that:

String s = new String();
+5
source share
3 answers

new String()- empty line. It can be a return value from a method, for example, if you have nothing to return. Using a literal ""is probably better because a literal empty string value will already be interned.

+7

This is an empty string equivalent "". From javadoc:

String, . , , .

EDIT:

. , true:

"" == ""
"" != new String()
"".equals(new String())
+4

Java, String ?

, , equal "", . , new String(str), str .

, , . , , , ( , Java Java).

+3

All Articles