Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Since Strings are immutable in Java, why would I want to use the argument-less String constructor and create an object?

How is the variable s useful to me after I do:

String s = new String();
like image 800
dumptoday Avatar asked Mar 10 '12 23:03

dumptoday


1 Answers

new String() is an empty string. This could be a return value from a method, for example, if you don't have anything more interesting to return. Using the literal "" is probably better because the literal empty string value will already be interned.

like image 71
Greg Hewgill Avatar answered Oct 20 '22 03:10

Greg Hewgill