What does the author try to tell us about the toString()
method in this excerpt:
The
toString()
method of theInteger
class is automatically used when a string of characters is needed in the next to last statement. ThetoString()
method of theDouble
class is automatically used when a string of characters is needed last statement.
when discussing this code:
Integer value = new Integer(103);
Double dvalue = new Double(-32.78);
System.out.println( "Integer object holds: " + value );
System.out.println( "Double object holds: " + dvalue );
The toString() method of the Integer class is automatically used when a string of characters is needed in the next to last statement.
There is code accompanying the text you quoted. The next to last statement in the code is:
System.out.println( "Integer object holds: " + value );
where value
is an instance of Integer
.
To perform the concatenation "Integer object holds: " + value
, the value
must be converted to a string. The toString()
method of Integer
is invoked to perform this conversion.
Similarly the statement:
System.out.println( "Double object holds: " + dvalue );
involves invoking the toString()
method of Double
to convert dvalue
to a string.