+ 1
Whatâs the return value of an instance?Will the String method be executed during the process of creating an instanceïŒ
Why the output Is x? class A{ int x; public A(int y) { x=y; } public String toString() { return âxâ; } } public class B{ public static void main(String[] args){ System.out.print(new A(10)); } }
2 Answers
+ 8
It's the object itself, Java tries to auto-typecast it to string by using its builtin toString (probably...)
0
I read an article occasionally and found that the point is the source code of toString method,when you print sth,it will call valueOf method and the valueOf will call toString method.Then class A overrides toString method,so it return âxâ.