2. Writing Advanced Expressions in Java
Previously, you learned how to write expressions to do basic calculations. In this section, you will learn how to use expressions to perform complex calculations.
Maximum length of an expression
So far, we have only been writing expressions with one or two operands. For instance, System.out.println(100 + 120)
.
The length of an expression is, for all practical purposes, unlimited.
Example 1
The following is an example of a long expression using integer values.
System.out.println(100 + 120 + 130 + 140 + 150 - 50 - 40 - 30);
Order of operations in expressions
Expressions follow the order of operations.
Multiplication and division
In the order of of operations, multiplication and division are performed before addition and subtraction.
Example 2
Show the steps used to evaluate the expression in the statement System.out.println(25 + 20 - 5 * 8 / 8)
Step | Result |
---|---|
Evaluate 5 * 8 | 25 + 20 - 40 / 8 |
Evaluate 40 / 8 | 25 + 20 - 5 |
Evaluate 25 + 20 | 45 - 5 |
Evaluate 45 - 5 | 40 |
Parentheses
Parentheses can be used in expressions to specify subexpressions. Within an expression, subexpressions are evaluated first, from left to right. In 5 + (10 + 20) * 3 + (5 - 2)
, 10 + 20
and 5 - 2
are the subexpressions.
Example 3
Show the steps that Java uses to evaluate the expression in the statement 5 + (10 + 20) * 3 + (5 - 2)
:
Step | Result |
---|---|
Evaluate (10 + 20) | 5 + 30 * 3 + (5 - 2) |
Evaluate (5 - 2) | 5 + 30 * 3 + 3 |
Evaluate 30 * 3 | 5 + 90 + 3 |
Evaluate 90 + 5 + 3 | 98 |
Problem 1
Show the steps that Java uses to evaluate the expression in the statement 5 - (8 * 3) + (5 - 2) * 10
:
This is an application of the previous example.
Evaluating method calls in expressions
Method calls contain parentheses, with one or more inputs between the parentheses. Each input is a subexpression.
Example 4
The show the steps used to evaluate the expression inside of the statement System.out.println(20 + Math.Max(10 - 15, 5 - 3));
.
Step | Result |
---|---|
Evaluate 10 - 15` | 20 + Math.Max(-5, 5 - 3) |
Evaluate 5 - 3` | 20 + Math.Max(-5, 2) |
Call Math.Max(-5, 2) | 2 |
Evaluate 20 + 2 | 22 |
From the examples above, all method input expressions are evaluated before the method is called.
Problem 2
Show the steps used to evaluate the expression inside of System.out.println(...)
for System.out.println(Math.Pow(25 - 20, 2 + 4))
:
This is an application of the previous example.
Nesting a chain of expressions
An expression, can be nested inside of another expression, which can be nested inside of another expression, and so on.
Example 5
Show the steps that Java uses to evaluate the expression in the statement System.out.println(50 - (100 * (20 - 15) - 5) + 80));
:
Step | Result |
---|---|
Start | 50 - (100 * (20 - 15) - 5) + 80 |
Look at (100 * (20 - 15) - 5) | 50 - (100 * (20 - 15) - 5) + 80 |
Evaluate (20 - 15) | 50 - (100 * 5 - 5 + 80) |
Evaluate 100 * 5 | 50 - (500 - 5 + 80) |
Evaluate 500 - 5 | 50 - (495 + 80) |
Evaluate 495 + 80 | 50 - 575 |
Evaluate 50 - 575 | -525 |
Problem 3
Show the steps that Java uses to evaluate the expression in the statement System.out.println(10 + Math.Max(10, 3 * 5 + (20 - 15 * (3 + 4)))
;
This is an implied application of previous examples in this module.