1. as followsCode: Use the this keyword to print the object address
Import java. util .*;
Public class infiniterecursion {
Public String tostring (){
Return "infiniterecursion address:" + This + "\ n ";
// Return "infiniterecursion address:" + super. tostring () + "\ n ";
}
Public static void main (string [] ARGs ){
List <infiniterecursion> V = new arraylist <infiniterecursion> ();
For (INT I = 0; I <1; I ++)
V. Add (New infiniterecursion ());
System. Out. println (v );
}
}///:~
Result: recursive tostring () method.
Explanation:
The compiler sees a string followed by a '+', but the object after '+' is not a string, so the compiler tries
Convert to the string type. This type of conversion operation calls the tostring () method and generates a recursive call.
In this example, if you really want to print the object address, you should call the tostring () method of the object.
Do this. Therefore, replace this with super. tostring.