且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Java 中使用 toString 方法?

更新时间:2023-10-06 13:47:34

来自 Object.toString 文档:

From the Object.toString docs:

返回一个字符串表示目的.一般来说,toString方法返回一个字符串文本表示"这个对象.结果应该是一个简洁但信息表示是一个人容易阅读.这是建议所有子类覆盖此方法.

Returns a string representation of the object. In general, the 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.

Object 类的 toString 方法返回一个字符串,由对象所属的类的名称是一个实例,at 符号字符`@' 和无符号的十六进制哈希码的表示目的.换句话说,这个方法返回一个等于值的字符串的:

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())

示例:

String[] mystr ={"a","b","c"};
System.out.println("mystr.toString: " + mystr.toString());

output:- mystr.toString: [Ljava.lang.String;@13aaa14a