新的String之差(的char [])和char []。的toString [英] Difference between new String(char[]) and char[].toString

查看:146
本文介绍了新的String之差(的char [])和char []。的toString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java以下两个code块的输出是不同的。我想知道为什么。

The output for following two code blocks in Java is different. 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();
    }

对不起。我的错!
通过比较两个字符串。
输出到第二个字符串看起来像这样被许多的建议 -
[C @ 2e0ece65

Sorry. My bad! Using to compare two strings. The output to second string looks like this as suggested by many - [C@2e0ece65

谢谢!

推荐答案

在Java中,的toString [,然后再字符presenting数组元素类型( C 在这种情况下),然后标识哈希code。所以你的情况,你确定它返回原来的字符串,而不是像 [C @ f4e6d

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. So in your case, are you sure it is returning the original string and not something like [C@f4e6d?

无论哪种方式,你应该使用新的String(ARR)。这是一个转换的char [] 字符串的最短,最巧妙,方式。你也可以使用的 Arrays.toString(ARR)

Either way, you should use new String(arr). This is the shortest, neatest, way of converting a char[] back to a String. You could also use Arrays.toString(arr)

相关琐事

这是你的 arr.toString()方法返回类似的原因[Cf4e6d 是的 Object.toString 返回

The reason that your arr.toString() method returns something like [Cf4e6d is that Object.toString returns

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

对于字符阵列,的getName()返回字符串 [C 。为了您的程序,你可以用code看到这一点:

For a char array, getName() returns the string [C. For your program you can see this with the code:

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

的结果, Object.hash code() ,返回一个基于对象的内存地址,而不是数组内容的一个数字。这是因为,通过默认的定义等于的对象是引用相等,即,两个对象是相同的,只有当他们是在存储器中的相同引用的对象。因此,你会得到不同的 arr.toString()值基于相同的字符串两个数组:

The second part of the result, Object.hashCode(), returns a number based on the object's memory address, not the array contents. This is because by default the definition of "equals" for an object is reference equality, i.e. 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:

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

给出了:

[C@4b7c8f7f
[C@5eb10190

请注意,这是字符串类,其中的平等规则的重写,使其具有价值相等的不同。但是,你应该总是使用的 string1.equals(字符串2) 测试字符串是否相等,而不是 == 作为在 == 方法仍将测试的内存位置。

Note that this is different for the String class where the equality rules are overridden to make it have value equality. However, you should always use string1.equals(string2) to test for string equality, and not == as the == method will still test for memory location.

这篇关于新的String之差(的char [])和char []。的toString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆