简体   繁体   English

对象如何隐式调用toString方法?

[英]How an object will call toString method implicitly?

如果我正在打印该类的对象,那么即使我没有编写toString()方法,它也在打印toString()方法的实现,那么实现是什么,它如何在内部调用toString()

You're not explicitly calling toString() , but implicitly you are: 您没有显式调用toString() ,但暗含的是:

See: 看到:

System.out.println(foo); // foo is a non primitive variable

System is a class, with a static field out , of type PrintStream . System是一类,具有staticout型的, PrintStream So you're calling the println(Object) method of a PrintStream . 因此,您正在调用PrintStreamprintln(Object)方法。

It is implemented like this: 它是这样实现的:

   public void println(Object x) {
       String s = String.valueOf(x);
       synchronized (this) {
           print(s);
           newLine();
       }
   }

As we see, it's calling the String.valueOf(Object) method. 如我们所见,它正在调用String.valueOf(Object)方法。
This is implemented as follows: 它的实现如下:

   public static String valueOf(Object obj) {
       return (obj == null) ? "null" : obj.toString();
   }

And here you see, that toString() is called. 在这里,您可以看到调用了toString()

Every object in Java IS-A (n) Object as well. Java IS-A (n) Object每个对象也是如此。 Hence, if a toString() implementation has not been provided by a class the default Object.toString() gets invoked automatically. 因此,如果类未提供toString()实现,则会自动调用默认的Object.toString()

Object.toString() 's default implementation simply prints the object's class name followed by the object's hash code which isn't very helpful. Object.toString()默认实现只是打印对象的类名,然后打印对象的哈希码,这不是很有用。 So, one should usually override toString() to provide a more meaningful String representation of an object's runtime state. 因此,通常应该重写toString()以提供对象运行时状态的更有意义的String表示。

even I am not writing the toString() method so what are the implementation,how it is calling toString() internally? 即使我没有写toString()方法,所以实现是什么,它如何在内部调用toString()?

toString() is one of the few methods (like equals() , hashCode() etc.) that gets called implicitly under certain programmatic situations like (just naming a few) toString()是为数不多的某些编程情况(例如仅举几个例子) 隐式调用的少数方法(如equals()hashCode()等)之一。

  • printing an object using println() 使用println()打印对象
  • printing a Collection of objects ( toString() is invoked on all the elements) 印刷Collection对象toString()被调用上的所有元素)
  • concatenation with a String (like strObj = "My obj as string is " + myObj; ) 与字符串连接(例如strObj = "My obj as string is " + myObj;

一切都继承自Object,因此如果尚未定义,则将调用Object上的toString

toString() method is present in Object class, so when u put obj in System.out.println(obj);, impliciyly it will call toString() present in Object class since every user created class will implicitly inherits Object class so as ur newly created class, that means that toString() is available in ur class so it will print something like for example: "PkgNamePackage.Classname@12cf4" However if u explicitely override toString method and give ur own implementation then it will written the string what ever u give in Overriden tostring method(); toString()方法存在于Object类中,因此,当您将obj放入System.out.println(obj);中时,由于每个用户创建的类都会隐式继承Object类,因此它将隐式调用Object类中存在的toString()。新创建的类,这意味着toString()在ur类中可用,因此它将显示类似以下内容:“ PkgNamePackage.Classname@12cf4”但是,如果u显式重写toString方法并提供您自己的实现,则它将字符串写为你曾经给过Overriden tostring method(); ex: 例如:

public class DogArray {
    @Override
    public String toString() {
        return "Im the newly created Object";
    }

    public static void main(String args[]) {
        DogArray d1 = new DogArray();
        System.out.println(d1);
    }
}

output: Im the newly created Object

在java中,对象类是每个类的超类。每当您将参数传递给system.out.println时,内部对象类将转换为字符串方法。它返回给定的类名@引用值,但根据我们的应用需求对象类到字符串方法将在集合和字符串类中重写。它返回其内容。

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

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