Difference between String.valueOf(Boolean) vs String

1k views Asked by At

Is there a difference in any ways (i.e performance, maintainability,...etc) between:

String str = String.valueOf(Boolean.TRUE);

vs

String str = "true";
1

There are 1 answers

2
Jan Larsen On BEST ANSWER
String str = "true";

is faster because it does not involve a function call ( except they may be optimized to the same)

I also consider it easier to read.

The valueOf methods are good for variables.

Constant strings are better defined like this though:

private static final String TRUE = "true";

That way they are constant and can be optimized for sure. So:

String str = TRUE;