简体   繁体   English

新String(char [])和char []。toString之间的区别

[英]Difference between new String(char[]) and char[].toString

The output for following two code blocks in Java is different. Java中以下两个代码块的输出是不同的。 I am trying to understand why. 我想知道为什么。

private String sortChars(String s){
      char[] arr = s.toCharArray(); //creating new char[]
       Arrays.sort(arr); //sorting that array
        return new String(arr);  
    }

This one returns a string with sorted characters as expected. 这个返回一个带有排序字符的字符串。

private String sortChars(String s){
      char[] arr = s.toCharArray(); //creating new char[]
       Arrays.sort(arr); //sorting that array
        return arr.toString();
    }

Sorry. 抱歉。 My bad! 我的错! Using to compare two strings. 用于比较两个字符串。 The output to second string looks like this as suggested by many - [C@2e0ece65 输出到第二个字符串看起来像许多人建议的那样 - [C @ 2e0ece65

Thanks! 谢谢!

In Java, toString on an array prints [ , then a character representing the array element type ( C in this case) and then the identity hash code. 在Java中,数组上的toString打印[ ,然后是表示数组元素类型的字符(在本例中为C ),然后是标识哈希码。 So in your case, are you sure it is returning the original string and not something like [C@f4e6d ? 所以在你的情况下,你确定它是返回原始字符串而不是像[C@f4e6d

Either way, you should use new String(arr) . 无论哪种方式,您都应该使用new String(arr) This is the shortest, neatest, way of converting a char[] back to a String . 这是将char[]转换回String的最短,最好的方法。 You could also use Arrays.toString(arr) 你也可以使用Arrays.toString(arr)

Related trivia 相关琐事

The reason that your arr.toString() method returns something like [Cf4e6d is that Object.toString returns 你的arr.toString()方法返回类似[Cf4e6dObject.toString返回

getClass().getName() + '@' + Integer.toHexString(hashCode())

For a char array, getName() returns the string [C . 对于char数组, getName()返回字符串[C For your program you can see this with the code: 对于您的程序,您可以使用以下代码查看:

System.out.println(arr.getClass().getName());

The second part of the result, Object.hashCode() , returns a number based on the object's memory address, not the array contents. 结果的第二部分Object.hashCode()根据对象的内存地址返回一个数字,而不是数组内容。 This is because by default the definition of "equals" for an object is reference equality, ie two objects are the same only if they are the same referenced object in memory. 这是因为默认情况下,对象的“等于”的定义是引用相等,即只有两个对象在内存中是相同的引用对象时才是相同的。 You will therefore get different arr.toString() values for two arrays based on the same string: 因此,您将基于相同的字符串为两个数组获取不同的arr.toString()值:

String s = "fdsa";
char[] arr = s.toCharArray();
char[] arr2 = s.toCharArray();
System.out.println(arr.toString());
System.out.println(arr2.toString());

gives: 得到:

[C@4b7c8f7f
[C@5eb10190

Note that this is different for the String class where the equality rules are overridden to make it have value equality. 请注意,对于String类,这是不同的,其中重写了相等规则以使其具有值相等性。 However, you should always use string1.equals(string2) to test for string equality, and not == as the == method will still test for memory location. 但是,您应该始终使用string1.equals(string2)来测试字符串相等性,而不是==因为==方法仍将测试内存位置。

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

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