Java中的'System.out.println()'和'toString()'之间的连接 [英] The connection between 'System.out.println()' and 'toString()' in Java

查看:506
本文介绍了Java中的'System.out.println()'和'toString()'之间的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java中的 System.out.println() toString()之间的关系是什么?例如:

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

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);
    }
}

如果主类运行, code>abc。当我删除覆盖 toString()的代码时,它给出一个输出A @ 659e0bfd。那么,任何人都可以解释当我使用参数作为一个对象时, System.out.println()的工作原理是什么?是否与 toString()方法完全连接?

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 use the parameter as an object? Is it fully connected with toString() method?

推荐答案

System.out PrintStream 。 Printstream定义了 println()函数的几个版本来处理数字,字符串等等。当以任意对象作为参数调用 PrintStream.println()时,您将获得作用于 Object 的函数版本。此版本的函数

System.out is 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


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

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

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

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


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

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

所以,长故事, out.println(someObject)调用该对象的 toString()函数将对象转换为字符串表示。

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

如果你的对象定义了自己的 toString()函数,那么就是被调用的。如果你不提供这样的函数,那么你的对象将从它的一个父类继承 toString()。在最坏的情况下,它将继承 Object.toString() 。该版本的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


一个字符串,包含对象为实例的类的名称,符号字符@以及对象的散列码的无符号十六进制表示。

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.

或者换句话说:

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

所以,当你调用 System.out.println()对象没有定义自己的版本的toString(),你可能会得到对象版本,看起来像classname @ someHexNumber。

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".

这篇关于Java中的'System.out.println()'和'toString()'之间的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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