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

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

问题描述

System.out.println()toString() 在 Java 中有什么联系?例如:

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

如果主类运行,它给出的输出为 "abc".当我删除覆盖 toString() 的代码时,它给出的输出为 "A@659e0bfd".那么,当我将 obj 对象引用作为参数传递给它时,谁能解释一下 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 pass the obj object reference as an argument to it? 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

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

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

所以,长话短说,System.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

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

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

因此,当您在未定义自己的 toString() 版本的对象上调用 System.out.println() 时,您可能会得到 Object看起来像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天全站免登陆