您的位置:首页 > 产品设计 > UI/UE

The comparison of the toString() method and the valueOf(...) method of the String class

2016-05-13 19:53 501 查看
toString() method of the String class

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method
returns a string equal to the value of: getClass().getName() + '@' + Integer.toHexString(hashCode())

valueOf(char[] data) method of the String class

Returns the string representation of the char array argument. The contents of the character array are copied; subsequent modification of the character array does not affect the newly created string.

For example:

char[] chs = {'0', '0', '0', '0', '0', '0', '2', '4'};

String strHex = "0x" + String.valueOf(chs);

System.out.println(strHex);

Output:0x00000024

String strTest = "0x" + chs.toString();

System.out.println(strTest);

Output:0x[C@71f801f7 (The Integer.toHexString(hashCode() may be different from this when it runs in your computer)

Plus: hashCode() method of the String class
Returns a hash code for this string. The hash code for a String object is computed as s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]

using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation. (The hash value of the empty string is zero.)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: