java Java中'System.out.println()'和'toString()'之间的联系

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29318996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:03:16  来源:igfitidea点击:

The connection between 'System.out.println()' and 'toString()' in Java

javaclassoverridingtostring

提问by anisotropic

What is the connection between System.out.println()and toString()in Java? e.g:

JavaSystem.out.println()toString()Java 中的联系是什么?例如:

public class A {
    String x = "abc";

    public String toString() {
        return x;
    }
}

public class ADemo {
    public static void main(String[] args) {
        A obj = new A();
        System.out.println(obj);
    }
}

If main class runs, it gives an output as "abc". When I remove the code which overrides toString(), it gives an output as "A@659e0bfd". So, can anyone explain what is the working principle of?System.out.println()?when I pass the objobject reference as an argument to it? Is it fully connected with toString()method?

如果主类运行,它给出的输出为"abc". 当我删除覆盖的代码时toString(),它给出的输出为"A@659e0bfd". 那么,谁能解释一下它的工作原理是什么?System.out.println()?当我将obj对象引用作为参数传递给它时?它与toString()方法完全相关吗?

回答by Kenster

System.outis a PrintStream. Printstream defines several versions of the println()function to handle numbers, strings, and so on. When you call PrintStream.println()with an arbitrary object as a parameter, you get the version of the function that acts on an Object. This version of the function

System.out是一个PrintStream。Printstream 定义了多个版本的println()函数来处理数字、字符串等。当您PrintStream.println()使用任意对象作为参数进行调用时,您将获得作用于Object. 这个版本的功能

...calls at first String.valueOf(x) to get the printed object's string value...

...首先调用 String.valueOf(x) 以获取打印对象的字符串值...

Looking at String.valueOf(Object), we see that it returns

String.valueOf(Object),我们看到它返回

if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.

如果参数为空,则字符串等于“空”;否则,返回 obj.toString() 的值。

So, long story short, System.out.println(someObject)calls that object's toString()function to convert the object to a string representation.

因此,长话短说,System.out.println(someObject)调用该对象的toString()函数将对象转换为字符串表示。

If your object defines its own toString()function, then that is what will be called. If you don't provide such a function, then your object will inherit toString()from one of its parent classes. In the worst case, it will inherit Object.toString(). That version of toString() is defined to return

如果您的对象定义了它自己的toString()函数,那么它将被调用。如果您不提供这样的函数,那么您的对象将从toString()其父类之一继承。在最坏的情况下,它将继承Object.toString(). 该版本的 toString() 被定义为返回

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.

一个字符串,由对象是其实例的类的名称、at 符号字符“@”和对象哈希码的无符号十六进制表示组成。

Or, in other words:

或者,换句话说:

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

So, when you call System.out.println()on an object that doesn't define its own version of toString(), you might get the Objectversion which looks like "classname@someHexNumber".

因此,当您调用System.out.println()未定义自己的 toString() 版本的对象时,您可能会得到Object类似于“classname@someHexNumber”的版本。

回答by CMPS

toString()is a method that exist in the Objectclass (Root of the inheritence tree) for all classes.

toString()Object所有类的类(继承树的根)中存在的方法。

System.out.print()(SOP) will call the toString method when fed an object.

System.out.print()(SOP) 将在馈送对象时调用 toString 方法。

If you don't overwrite the method toString(), SOP will call the parent toString()which, if parent is the Object class, it will print the hashCode of the object

如果不覆盖该方法toString(),SOP 将调用父toString()类,如果父类是 Object 类,它将打印对象的 hashCode

If you overwrite the method, SOP will call your toString()method

如果您覆盖该方法,SOP 将调用您的toString()方法

回答by sam schonstal

System.out.println(obj) will print the returned string from obj.toString() if you dont override it it will call the base object.toString() method which by default 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:

System.out.println(obj) 将打印从 obj.toString() 返回的字符串,如果您不覆盖它,它将调用基础 object.toString() 方法,默认情况下,类 Object 的 toString 方法返回一个由以下内容组成的字符串对象是其实例的类的名称,符号字符“@”,以及对象哈希码的无符号十六进制表示。换句话说,此方法返回一个等于以下值的字符串:

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