如何在 Java 中使用 toString 方法? [英] How to use the toString method in Java?

查看:26
本文介绍了如何在 Java 中使用 toString 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能向我解释在Object 类中定义的toString() 方法的概念?它是如何使用的,它的目的是什么?

Can anybody explain to me the concept of the toString() method, defined in the Object class? How is it used, and what is its purpose?

推荐答案

来自 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

这篇关于如何在 Java 中使用 toString 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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