简体   繁体   English

字符串valueof(char [])和新字符串(char [])之间的区别

[英]difference between string valueof(char[]) and new string(char[])

The output for following print statement is same, is there any internal difference which is safe as per Privacy Violation: Heap Inspection 以下打印声明的输出是相同的,是否存在任何内部差异,按照“隐私冲突”的规定是安全的:堆检查

char[] ch ={'p','a','s','s','w','o','r','d'}; char [] ch = {'p','a','s','s','w','o','r','d'};

System.out.println(String.valueOf(ch));

System.out.println(new String(ch));

There is no difference 没有区别

valueOf is static factory method which calls String constructor valueOf是静态工厂方法,它调用String构造函数

There is no real difference because internal implementation of valueOf is the following: 没有真正的区别,因为valueOf内部实现如下:

public static String valueOf(char data[]) {
    return new String(data);
}

As you can see it calls directly new String(data) 如您所见,它直接调用new String(data)

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

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