Displaying the Result of the Calculation
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
Get Android™ How to Program, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.