简体   繁体   English

String.valueOf(Boolean)与String之间的区别

[英]Difference between String.valueOf(Boolean) vs String

Is there a difference in any ways (ie performance, maintainability,...etc) between: 之间在任何方面(例如,性能,可维护性等)是否存在差异:

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

vs VS

String str = "true";
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. valueOf方法适用于变量。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM