gpt4 book ai didi

java - java 是否优化字符串文字 toLowerCase()?

转载 作者:搜寻专家 更新时间:2023-11-01 01:49:24 29 4
gpt4 key购买 nike

java 是否优化了字符串文字的操作?例如,确实

"literal".toLowerCase()

总是创建一个新的字符串实例?

最佳答案

toLowerCase() 调用 toLowerCase(Locale.getDefault())

查看实现,您会发现如果不需要更改任何字符,则返回原始 String:

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 - java 是否优化字符串文字 toLowerCase()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47012268/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com