Java是否将字符串文字优化为toLowerCase()? [英] Does java optimize string literal toLowerCase()?
本文介绍了Java是否将字符串文字优化为toLowerCase()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
java是否使用字符串文字优化操作?例如,
Does java optimize operations with string literals? For example, does
"literal".toLowerCase()
总是创建一个新的字符串实例吗?
always create a new string instance?
推荐答案
toLowerCase()
调用toLowerCase(Locale.getDefault())
.
查看实现,您会发现如果不需要更改任何字符,则会返回原始的String
:
Looking at the implementation you'll see that the original String
is returned if no characters need to be changed:
public String toLowerCase(Locale locale) {
if (locale == null) {
throw new NullPointerException();
}
int firstUpper;
final int len = value.length;
/* Now check if there are any characters that need to be changed. */
scan: {
for (firstUpper = 0 ; firstUpper < len; ) {
char c = value[firstUpper];
if ((c >= Character.MIN_HIGH_SURROGATE)
&& (c <= Character.MAX_HIGH_SURROGATE)) {
int supplChar = codePointAt(firstUpper);
if (supplChar != Character.toLowerCase(supplChar)) {
break scan;
}
firstUpper += Character.charCount(supplChar);
} else {
if (c != Character.toLowerCase(c)) {
break scan;
}
firstUpper++;
}
}
return this; // the original String is returned
}
...
}
这篇关于Java是否将字符串文字优化为toLowerCase()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文