Section 2.7 Worked Example: Basic Operation Precedence
Subgoals for evaluating an assignment statement.
- Determine resultant data type of expression
- Update variable for pre operators based on side effect
- Solve arithmetic equation, operator precedence
- Is the Left Hand Side (LHS) of the assignment statement a variable? Check the data type of the value on right hand side (RHS) against data type of LHS variable.
- Update variable for post operators based on side effect
Subsection 2.7.1
You can watch this video or read through the content below it.
Given the following code snippet, evaluate the final statement (the last line). If invalid, give the reason. If valid, what value is assigned to the variable? Note any possible side effects.
int alpha = 2;
int beta = 1;
int delta = 4;
int eta, gamma;
double omega = 2.5;
double theta = -1.3;
double kappa = 3.0;
double lambda, rho;
lambda = alpha / kappa + delta;
Subsection 2.7.2 SG1 : Determine resultant data type of expression
The expression is the right-hand-side (RHS) of the statement:
alpha / kappa + delta
. The variables alpha and delta were declared to be the int
data type, while kappa was declared to be a double
data type. Division has higher precedence than summation, In looking at the division operation, the numberator (top number) is declared to be an integer and the denominator (bottom number) is declared to be a double. Operations with mixed data types will always yield the data type without a loss of precision. Operations with an int and a double will yield a double. The second operation is summation which now involves a double and an integer. The resultant data type of the expression on the RHS is double. promoted for a valid summation with the result of alpha / kappa.Subsection 2.7.3 SG2: Update variable for pre-increment or pre-decrement operators (side effect)
NOT USED IN THIS EXAMPLE
Subsection 2.7.4 SG3: Evaluate arithmetic expression according to operator precedence
In the declarations, alpha was initialized with a value of 2, delta was initialized with a value of 4, and kappa was initilized with a value of 3.0.
The RHS of the statement is
alpha / kappa + delta
, so we replace those variable names with their actual values 2 / 3.0 + 4
. Division has higher precedence than summation, so we first evaluate 2 / 3.0 as 0.6667 (we are rounding to 4 decimals for convenience – a complete study of floating-point precision in Java is out of the scope of this lesson). After the division, we can now add 0.6667 + 4 for a total of 4.6667.Subsection 2.7.5 SG4: Is the Left Hand Side (LHS) of the assignment statement a variable? Check the data type of the value on right hand side (RHS) against data type of LHS variable.
The LHS is a variable of type
double
, and the RHS is type double
. This is valid.Subsection 2.7.6 SG5: Update variable for post-increment or post-decrement operators (side effect)
NOT USED IN THIS EXAMPLE
Subsection 2.7.7 Questions to check understanding
int alpha = 2;
int delta = 4;
double kappa = 3.0;
double lambda;
lambda = alpha / kappa + delta;
Q1) In line 5 above, is the LHS of the statement a variable? What data type?
Q2) What is the resulting data type after evaluating the RHS of line 5?
Q3) Can the data type of the RHS result be assigned to the LHS variable in line 5?
Answer.
Q1-Answer) Yes, lambda is declared as a double
Q2-Answer) On the RHS, alpha / kappa + delta is evaluated as int / double + int which is double
Q3-Answer) Yes, a double can be assigned to a double
Practice Pages.
You have attempted of activities on this page.