Why did the program print the first "40" without a decimal point, but printed the second one with a decimal point as "40.0" ?
The first value was stored in a variable of data type long, an integer type. Integers do not have fractional parts. The second forty was the result of a calculation involving a variable of data type double, a floating point type.
Here is the program again:
class example
{
public static void main ( String[] args )
{
long hoursWorked = 40;
double payRate = 10.0, taxRate = 0.10;
System.out.println("Hours Worked: " + hoursWorked );
System.out.println("pay Amount : " + (hoursWorked * payRate) );
System.out.println("tax Amount : " + (hoursWorked * payRate * taxRate) );
}
}
Look carefully at the statement highlighted in red.
The parentheses around (hoursWorked * payRate)
show that we want to multiply hoursWorked
by payRate
and then to append the
result (converted to characters) to the string.
When you have a calculation as part of a
System.out.println()