The following is my java code snippet:
static String sortChars(String s) {
char[] chars = s.toCharArray();
Arrays.sort(chars);
return chars.toString();
}
I invoke above function by using:
String result = sortChars(s);
But the result does not meet my expectation:for example,the s=”are”, the result=”aer”. However, when I use:
return new String(chars)
It works.
Could somebody tell me the reason of it. Thanks
Since
char[]class does not override the defaultObject‘stoString()implementation, it does not return a string composed by the characters in the char array, but the char[] class name + hash code. For example:arr[C@19821f.