Evaluating Expressions and Operator Precedence
Tags: learning, java, programming, beginners
Published: 2024-04-24 15:12:19

Java expressions are evaluated in the same way as arithmetic expressions. Writing a numeric expression in Java involves a straightforward translation of an arithmetic expression using Java operators.

Though Java has its own way to evaluate an expression behind the scene, the result of a Java expression and its corresponding arithmetic expression is the same. Therefore, you can safely apply the arithmetic rule for evaluating a Java expression. Operators contained within pairs of parentheses are evaluated first. Parentheses can be nested, in which case the expression in the inner parentheses is evaluated first. When more than one operator is used in an expression, the following operator precedence rule is used to determine the order of evaluation.

- Multiplication, division, and remainder operators are applied first. If an expression contains several multiplication, division, and remainder operators, they are applied from left to right.
- Addition and subtraction operators are applied last. If an expression contains several addition and subtraction operators, they are applied from left to right.

Here's a program that converts a Fahrenheit degree to Celsius using the formula celsius = (5 / 9)(fahrenheit - 32).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y52xuhjuexeghb399gpt.png)

Be careful when applying division. Division of two integers yields an integer in Java. 5 / 9 is translated to **5.0 / 9** instead of **5 / 9**, because **5 / 9** yields **0** in Java.

## Case Study: Displaying the Current Time

You can invoke **System.currentTimeMillis()** to return the current time. The problem is to develop a program that displays the current time in GMT (Greenwich Mean Time) in the format hour:minute:second, such as 13:19:8. The **currentTimeMillis** method in the **System** class returns the current time in milliseconds elapsed since midnight, January 1, 1970 GMT. This time is known as the _UNIX epoch_. The epoch is the point when time starts, and **1970** was the year when the UNIX operating system was formally introduced.

You can use this method to obtain the current time, and then compute the current second, minute, and hour as follows.

1. Obtain the total milliseconds since midnight, January 1, 1970, in **totalMilliseconds** by invoking **System.currentTimeMillis()** (e.g., **1203183068328** milliseconds).
2. Obtain the total seconds **totalSeconds** by dividing **totalMilliseconds** by 1000 (e.g., **1203183068328** milliseconds / **1000** = **1203183068** seconds).
3. Compute the current second from **totalSeconds % 60** (e.g., **1203183068** seconds **% 60** = **8**, which is the current second).
4. Obtain the total minutes **totalMinutes** by dividing **totalSeconds** by **60** (e.g., **1203183068** seconds **/ 60** = **20053051** minutes).
5. Compute the current minute from **totalMinutes % 60** (e.g., **20053051** minutes **% 60** = **31**, which is the current minute).
6. Obtain the total hours **totalHours** by dividing **totalMinutes** by **60** (e.g., **20053051** minutes **/ 60** = **334217** hours).
7. Compute the current hour from **totalHours % 24** (e.g., **334217** hours **% 24** = **17**, which is the current hour).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t6nerntur3rjh6hdqkbc.png)