java 对象如何隐式调用 toString 方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17051481/
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
How an object will call toString method implicitly?
提问by user2475808
If I am printing an object of the class then it is printing the toString()
method implementation even I am not writing the toString()
method so what are the implementation,how it is calling toString()
internally?
如果我正在打印类的对象,那么toString()
即使我没有编写toString()
方法,它也会打印方法实现,那么实现是什么,它是如何在toString()
内部调用的?
回答by jlordo
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
. So you're calling the println(Object)
method of a PrintStream
.
System
是一个类,有一个static
字段out
,类型为PrintStream
。所以你正在调用 a 的println(Object)
方法PrintStream
。
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.
This is implemented as follows:
正如我们所见,它正在调用String.valueOf(Object)
方法。
这是按如下方式实现的:
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
And here you see, that toString()
is called.
你看,这toString()
就是所谓的。
回答by Ravi Thapliyal
Every object in Java IS-A(n) Object
as well. Hence, if a toString()
implementation has not been provided by a class the default Object.toString()
gets invoked automatically.
Java IS-A(n) 中的每个对象Object
也是如此。因此,如果一个toString()
类没有提供一个实现,默认值Object.toString()
会被自动调用。
Object.toString()
's default implementationsimply prints the object's class name followed by the object's hash code which isn't very helpful. So, one should usually override toString()
to provide a more meaningful String representation of an object's runtime state.
Object.toString()
的默认实现只是打印对象的类名,后跟对象的哈希码,这不是很有帮助。因此,通常应该重写toString()
以提供对象运行时状态的更有意义的字符串表示。
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 implicitlyunder certain programmatic situations like (just naming a few)
toString()
是在某些编程情况下被隐式调用的少数方法之一(例如equals()
,hashCode()
等),例如(仅命名一些)
- printing an object using
println()
- printing a
Collection
of objects (toString()
is invoked on all the elements) - concatenation with a String (like
strObj = "My obj as string is " + myObj;
)
- 使用打印对象
println()
- 打印一个
Collection
对象(toString()
在所有元素上调用) - 与字符串连接(如
strObj = "My obj as string is " + myObj;
)
回答by NimChimpsky
回答by Mallikarjuna
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(); ex:
toString() 方法存在于 Object 类中,因此当您将 obj 放入 System.out.println(obj); 时,它会隐式调用存在于 Object 类中的 toString(),因为每个用户创建的类都将隐式继承 Object 类,因此您新创建的类,这意味着 toString() 在您的类中可用,因此它将打印如下内容:“PkgNamePackage.Classname@12cf4” 但是,如果您明确覆盖 toString 方法并提供您自己的实现,那么它将写入字符串你曾经在 Overriden tostring method() 中给出过;前任:
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
回答by avinashnarisetty
In java object class is super class to the each and every class.whenever your passing parameter to the system.out.println internally object class to string method will be excuted.it returns class name@reference value given but as per our application requirement object class to string method will override in collection and string class.it returns their content.
在java对象类是每个类的超类。无论何时您将参数传递给system.out.println内部对象类到字符串方法将被执行。它返回给定的类名@reference值,但根据我们的应用程序要求对象类到字符串方法将覆盖集合和字符串类。它返回它们的内容。