[JAVA]数组转换成字符串,java字符串
分享于 点击 3253 次 点评:279
[JAVA]数组转换成字符串,java字符串
所有类都继承自Object类,Object里有一个方法就是toString(),那么所有的类创建的时候,都有一个toString的方法。
Object类中的toString()方法的源代码如下:
/**
* Returns a string representation of the object. In general, the
* {@code toString} method returns a string that
* "textually represents" this object. The result should
* be a concise but informative representation that is easy for a
* person to read.
* It is recommended that all subclasses override this method.
* <p>
* The {@code toString} method for class {@code Object}
* returns a string consisting of the name of the class of which the
* object is an instance, the at-sign character `{@code @}', 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:
* <blockquote>
* <pre>
* getClass().getName() + '@' + Integer.toHexString(hashCode())
* </pre></blockquote>
*
* @return a string representation of the object.
*/
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
因为数组类中并没有对toString方法的重写(override),仅仅是重载(overload)为类的静态方法(参见java.util.Arrays)。所以,数组直接使用toString()的结果也是[类型@哈希值]。
所以数组转为字符串应写成:Arrays.toString(A)
参考资料:http://www.cnblogs.com/ningvsban/p/3955483.html
相关文章
- 暂无相关文章
用户点评