Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java toUpperCase()方法何时创建新对象?_Java_String_Object - Fatal编程技术网

Java toUpperCase()方法何时创建新对象?

Java toUpperCase()方法何时创建新对象?,java,string,object,Java,String,Object,输出:true toUpperCase()总是创建一个新对象吗?toUpperCase()调用toUpperCase(Locale.getDefault()),它只在必须时创建一个新的字符串对象。如果输入的字符串已经是大写,它将返回输入的字符串 不过,这似乎是一个实现细节。我没有在Javadoc中找到它 下面是一个实现: public class Child{ public static void main(String[] args){ String x = new

输出:
true

toUpperCase()
总是创建一个新对象吗?

toUpperCase()
调用
toUpperCase(Locale.getDefault())
,它只在必须时创建一个新的
字符串
对象。如果输入的
字符串
已经是大写,它将返回输入的
字符串

不过,这似乎是一个实现细节。我没有在Javadoc中找到它

下面是一个实现:

public class Child{

    public static void main(String[] args){
        String x = new String("ABC");
        String y = x.toUpperCase();

        System.out.println(x == y);
    }
}
公共字符串toUpperCase(区域设置){
if(locale==null){
抛出新的NullPointerException();
}
int firstLower;
最终整数长度=value.length;
/*现在检查是否有任何字符需要更改*/
扫描:{
对于(firstLower=0;firstLower=Character.MIN\u HIGH\u代理)

&&(c我不会依赖这种行为,但我希望它能够避免创建新对象。注意:新字符串(…)不会更改答案。字符串x=新字符串(“ABC”);请不要这样做。您创建字符串对象两次。只需使用x=“ABC”;编辑:有人指出OP使用了新字符串(“ABC”)指出这不是实习的事实。我不认为实习和不实习对toUpperCase(地区)有什么不同method@Sarief如果它创建了一个新对象(并返回了该新对象)
x==y
肯定会返回false。@Sarief它以某种方式到达,这往往会导致高流量。@Eran让我重新表述一下,即使它在合同中创建了一个新对象,并且确实创建了一个新对象,但这并不意味着它将返回一个新对象。字符串包含在字符串池中,并且主要从那里重用@Sarief首先是一个不愚蠢的问题,因为字符串是不可变的,所以执行的任何操作都会导致一个单独的对象。其次,答案正如许多人所认为的那样与字符串池中的同一个字符串有关,但它不是因为OP使用
new
创建了字符串,而不是调用
intern()
因此,答案正确地指出了实现B的结果,即为什么没有为question@nits.kk这不适用于不可变对象,因为字符串是
public String toUpperCase(Locale locale) {
    if (locale == null) {
        throw new NullPointerException();
    }

    int firstLower;
    final int len = value.length;

    /* Now check if there are any characters that need to be changed. */
    scan: {
        for (firstLower = 0 ; firstLower < len; ) {
            int c = (int)value[firstLower];
            int srcCount;
            if ((c >= Character.MIN_HIGH_SURROGATE)
                    && (c <= Character.MAX_HIGH_SURROGATE)) {
                c = codePointAt(firstLower);
                srcCount = Character.charCount(c);
            } else {
                srcCount = 1;
            }
            int upperCaseChar = Character.toUpperCaseEx(c);
            if ((upperCaseChar == Character.ERROR)
                    || (c != upperCaseChar)) {
                break scan;
            }
            firstLower += srcCount;
        }
        return this; // <-- the original String is returned
    }
    ....
}