简体   繁体   中英

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'};

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

There is no real difference because internal implementation of valueOf is the following:

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

As you can see it calls directly new String(data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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