"....when a string of character is needed in the next to last statement" in toString() explanation

Sprint :

What does the author try to tell us about the toString() method in this excerpt:

The toString() method of the Integer class is automatically used when a string of characters is needed in the next to last statement. The toString() method of the Double 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 );
khelwood :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=328830&siteId=1