Java Reference
In-Depth Information
firstNum secondNum
ch
z
2.5
secondNum = console.
nextInt();
Read a number from the keyboard
(which is 8 ) and store it
After
Statement 5
4
8
A
into
2
secondNum .
This statement replaces the old value of
secondNum with the new value.
z = console.nextDouble();
Read a number from the keyboard
(which is 16.3 ) and store this
number into z . This statement
replaces the old value of z with
the new value.
After
Statement 6
firstNum secondNum
ch
z
16.3
4
8
A
After
Statement 7
firstNum secondNum
ch
z
16.3
firstNum = ( int )(z) + 8;
( int )(z) + 8 =
( int )(16.3) + 8 = 16 +
8 = 24 . Store 24 into
firstNum . This statement
replaces the old value of firstNum
with the new value.
24
8
A
After
Statement 8
firstNum secondNum
ch
z
16.3
secondNum = secondNum + 1;
secondNum + 1 = 8 + 1 =
9 . Store 9 into secondNum .
24
9
A
After
Statement 9
firstNum secondNum
ch
z
16.3
ch = console.next().
charAt(0);
Read the next input from the keyboard
(which is D ) and store it into ch .
This statement replaces the old value
of ch with the new value.
24
9
D
After
Statement 10
firstNum secondNum
ch
z
16.3
firstNum = firstNum +
( int )(ch);
firstNum + ( int )(ch) =
24 + ( int )('D') = 2 4 +
68 = 92 . Store 92 into
firstNum .
92
9
D
To access a Java program that shows the effect of the 10 statements listed at
the beginning of Example 2-18, download the Additional Student Files from
www.cengagebrain.com. The program is named Example2_18.java .
If you assign the value of an expression that evaluates to a floating-point value—without
using the cast operator—to a variable of type int , then a (syntax) error will occur.
 
Search WWH ::




Custom Search