Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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));
like image 489
Prathamesh Ketgale Avatar asked Oct 23 '25 16:10

Prathamesh Ketgale


1 Answers

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)

like image 66
Davide Lorenzo MARINO Avatar answered Oct 26 '25 04:10

Davide Lorenzo MARINO



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!