January 2014
Intermediate to advanced
736 pages
70h 43m
English
After the calculation has been performed, line 25 uses method System.out.printf to display the sum. The format specifier %d is a placeholder for an int value (in this case the value of sum)—the letter d stands for “decimal integer.” The remaining characters in the format string are all fixed text. So, method printf displays "Sum is ", followed by the value of sum (in the position of the %d format specifier) and a newline.
Calculations can also be performed inside printf statements. We could have combined the statements at lines 23 and 25 into the statement
System.out.printf( "Sum is %d\n", ( number1 + number2 ) );
The parentheses around the expression number1 + number2
Read now
Unlock full access