Java Reference
In-Depth Information
Also suppose that the following statements execute in the order given:
1.
firstNum = 4;
2.
secondNum = 2 * firstNum + 6;
3.
z = (firstNum + 1) / 2.0;
4.
ch = 'A';
5.
secondNum = console.nextInt();
6.
z = console.nextDouble();
7.
firstNum = (
int
)(z) + 8;
8.
secondNum = secondNum + 1;
9.
ch = console.next().charAt(0);
10.
firstNum = firstNum + (
int
)(ch);
In addition, suppose the input is:
8 16.3 D
Let's now determine the values of the declared variables after the last statement executes.
To show explicitly how a particular statement changes the value of a variable, the values
of the variables after each statement executes are shown. (In the following table, a
question mark,
?
, in a box indicates that the value in the box is unknown.)
Values
of the
Variables
Variables
Statement/Explanation
firstNum secondNum
ch
z
?
Before
Statement 1
?
?
?
firstNum secondNum
ch
z
?
After
Statement 1
firstNum = 4;
Store
4
into
firstNum
.
4
?
?
firstNum secondNum
ch
z
?
After
Statement 2
secondNum = 2 *
firstNum + 6;
2 * firstNum + 6 =
2 * 4 + 6 = 14
.
Store
14
into
secondNum
.
4
14
?
firstNum secondNum
ch
z
2.5
z = (firstNum + 1) / 2.0;
(firstNum +
1
) / 2.0 =
(4 + 1) / 2.0 = 5 /
2.0 = 2.5
.
Store
2.5
into
z
.
After
Statement 3
4
14
?
firstNum secondNum
ch
z
2.5
After
Statement 4
ch = 'A';
Store
'A'
into
ch
.
4
14
A
Search WWH ::
Custom Search