What is the result of compiling and running the following code ?
public class Main{ static int si = 10; public static void main (String args []){ new Main (); }// ww w .j a v a 2s.co m public Main (){ System.out.println (this); } public String toString (){ return "Main.si = "+this.si; } }
Select 1 option
toString()
method.Main@nnnnnnnn
, where nnnnnnn
is the hash code of the Main object referred to by 'this'.Correct Option is : D
The toString method for class Object returns 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.
This method returns a string equal to the value of:.
getClass ().getName () + '@' + Integer.toHexString (hashCode ())