简体   繁体   English

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

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

What is the connection between System.out.println() and toString() in Java? Java 中System.out.println()toString()之间的联系是什么? eg:例如:

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" .如果主类运行,它给出的输出为"abc" When I remove the code which overrides toString() , it gives an output as "A@659e0bfd" .当我删除覆盖toString()的代码时,它给出的输出为"A@659e0bfd" So, can anyone explain what is the working principle of System.out.println() when I pass the obj object reference as an argument to it?那么,当我将obj对象引用作为参数传递给它时,谁能解释一下System.out.println()的工作原理是什么? Is it fully connected with toString() method?它是否与toString()方法完全连接?

System.out is a PrintStream . System.out是一个PrintStream Printstream defines several versions of the println() function to handle numbers, strings, and so on. Printstream 定义了println()函数的多个版本来处理数字、字符串等。 When you call PrintStream.println() with an arbitrary object as a parameter, you get the version of the function that acts on an Object .当您使用任意对象作为参数调用PrintStream.println()时,您将获得作用于Object的函数版本 This version of the function这个版本的功能

...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.如果您的对象定义了自己的toString()函数,那么它将被调用。 If you don't provide such a function, then your object will inherit toString() from one of its parent classes.如果您不提供这样的函数,那么您的对象将从其父类之一继承toString() In the worst case, it will inherit Object.toString() .在最坏的情况下,它将继承Object.toString() That version of toString() is defined to return该版本的 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 Object version which looks like "classname@someHexNumber".因此,当您在未定义自己的 toString() 版本的对象上调用System.out.println()时,您可能会得到类似于“classname@someHexNumber”的Object版本。

toString() is a method that exist in the Object class (Root of the inheritence tree) for all classes. toString()是所有类都存在于Object类(继承树的根toString()中的方法。

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() ,如果 parent 是 Object 类,它将打印对象的 hashCode

If you overwrite the method, SOP will call your toString() method如果您覆盖该方法,SOP 将调用您的toString()方法

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. System.out.println(obj) 将打印从 obj.toString() 返回的字符串,如果您不覆盖它,它将调用基础 object.toString() 方法,默认情况下,类 Object 的 toString 方法返回一个由以下内容组成的字符串对象是其实例的类的名称,符号字符“@”,以及对象哈希码的无符号十六进制表示。 In other words, this method returns a string equal to the value of:换句话说,此方法返回一个等于以下值的字符串:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM