Solution Review 2: Expression Evaluation
Have a look at the solution to the 'Expression Evaluation' challenge.
We'll cover the following...
Press + to interact
class ExpressionEvaluattion{public static void main(String args[]){int x = 2; // x = 2int y = 3; // y = 3, x = 2double z = 5; // z = 5.0, y = 3, x = 2x *= y; // z = 5.0, y = 3, x = 2*3 = 6y = x + y + (int) z; // z = 5.0, y = 6+3+5 = 14, x = 6z = x + y + y / z; // z = 6+14+(14/5.0) = 22.8, y = 14, x = 6y++; // z = 22.8, y = 15, x = 6x--; // z = 22.8, y = 15, x = 5x = (int) z + y * x / y % x; // x = 22 + ((15*5) / 15)) % 5 = 22System.out.println(x);}}
Explanation
Let’s go over the code line by line and keep track of each of the three variables.
-
In lines 5-7, we have three variables declared; variables
x
andy
are ofint
type, whereas variablez
is ofdouble
type. -
Now, look at line 9. Remember that
*=
is shorthand for the arithmetic operation of multiplying ...