Quiz yourself: Addition, subtraction, multiplication, and division in Java

December 1, 2021 | 3 minute read
Text Size 100%:

What happens when you divide an int by an int? Let’s find out.

Download a PDF of this article
More quiz questions available here

Given the expression

var res = 54/5*5-4.0+4*8/10;

What is the result of this calculation? Choose one.

A. 1.0
B. 1.36
C. 49.0
D. 53.0
E. 53.2

Answer. Notice that most of the operands in the expression shown in the question are int type literals, but one of them is a double. Java’s numeric operators produce a result type that is at least an int, so adding two short values creates an int, even if the result would fit in a short.

If either operand is wider than int, the type of the result is the type of the wider operand. Thus, adding an int and a float results in a float. Based on this description, the width of a type increases going from int to long, from long to float, and from float to double.

Unlike with some languages (such as Python), if you perform division with the forward slash (/), these rules still apply. Therefore, dividing two operands of int type results in an int and not a floating point type, which means that division of integer types will truncate any fractional result. This can be a critically important observation; failure to understand this behavior can create bugs that can be hard to spot, particularly if the division is buried in a larger expression.

Now, in Java math, operands are evaluated from left to right, regardless of the order of operation execution. In this particular quiz question, the operands are all literals, so this is not relevant. What is relevant, however, is the order of execution of the operations themselves, and the order is defined by precedence rules.

Interestingly, the Java specification does not document the precedence rules directly. Instead, you must infer the order from the syntax diagrams. Note, though, that multiplicative operations take precedence over additive ones, and within each of those bands, evaluation is left to right.

(Although the language specification does not describe the precedence rules, the Java tutorials do provide the information.)

If you rewrite the original expression using explicit parentheses, as follows, you can identify and emphasize the order in which this calculation is performed:

var res = ((54/5)*5)-4.0+((4*8)/10);

The expression can be seen to go through the following intermediate stages like this:

First stage: 54/5 produces 10; remember this is integer division. Then, 4*8 gives 32. Using boldface to identify the substitutions, the expression reduces to

(10*5)-4.0+(32/10)

Second stage: 10*5 gives 50 and 32/10 produces 3; this is another integer division. Now the expression further reduces to

50-4.0+3

Third stage: The additive operations are performed from left to right. The first operation is 50-4.0, and since at least one operand is double, the result is double. This expression reduces to

46.0+3

Fourth stage: The int operand is promoted to double to produce the final result.

49.0

As you can see, option C is correct, and options A, B, D, and E are incorrect.

Conclusion. The correct answer is option C.

Related quizzes

Simon Roberts

Simon Roberts joined Sun Microsystems in time to teach Sun’s first Java classes in the UK. He created the Sun Certified Java Programmer and Sun Certified Java Developer exams. He wrote several Java certification guides and is currently a freelance educator who publishes recorded and live video training through Pearson InformIT (available direct and through the O’Reilly Safari Books Online service). He remains involved with Oracle’s Java certification projects.

Mikalai Zaikin

Mikalai Zaikin is a lead Java developer at IBA Lithuania (part of worldwide IBA Group) and currently located in Vilnius. During his career, Zaikin has helped Oracle with development of Java certification exams, and he has been a technical reviewer of several Java certification books, including three editions of the famous Sun Certified Programmer for Java study guides by Kathy Sierra and Bert Bates.


Previous Post

Quiz yourself: Functional interfaces and error handling in Java

Mikalai Zaikin | 5 min read

Next Post


Arm resources show how to optimize your Java applications for AArch64

Alan Zeichick | 4 min read