Java Reference
In-Depth Information
TABLE 2-4 Values of the Variables num1 , num2 , and num3
Values of the
Variables
Variables
Statement/Explanation
num1
num2
num3
Before
Statement 1
?
?
?
num1
num2
num3
num1 = 18;
Store 18 into num1 .
After
Statement 1
18
?
?
num1 = num1 + 27;
num1
num2
num3
After
Statement 2
num1 + 27 = 18 + 27 = 45.
This value is assigned to
num1 , which replaces the
old value of num1 .
45
?
?
num2 = num1;
num1
num2
num3
After
Statement 3
Copy the value of num1
into num2 .
45
45
?
num3 = num2 / 5;
num1
num2
num3
After
Statement 4
45
45
9
num2 / 5 = 45 / 5 = 9.
This value is assigned to
num3 .So num3 = 9 .
num3 = num3 / 4;
num1
num2
num3
After
Statement 5
num3 / 4 = 9 / 4 = 2.
This value is assigned to
num3 , which replaces the
old value of num3 .
45
45
2
Thus, after the execution of the statement in Line 5, num1 = 45 , num2 = 45 ,and num3 = 2 .
The Java language is strongly typed, which means that you cannot assign a value
to a variable that is not compatible with its data type. For example, a string cannot
be stored in an int variable. If you try to store an incompatible value in a variable,
an error is generated when you compile the program or during program execution.
Therefore, in an assignment statement, the expression on the right side must evaluate
to a value compatible with the data type of the variable on the left side.
Search WWH ::




Custom Search