Evaluating Expressions and Operator Precedence

Evaluating Expressions and Operator Precedence

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).

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.

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

Leave a Reply

Your email address will not be published. Required fields are marked *