Arithmetic Operators

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

What is the value of: 12 - 4/2 + 2

Answer:

12, since the expression means: 12 - 2 + 2

Arithmetic Operators

An arithmetic operator is a symbol that asks for doing some arithmetic. As the previous question illustrates, if several operators are used in an expression, the operations are done in a specific order. Operators of higher precedence are done first. The table shows just a few of the many operators in Java.

OperatorMeaningprecedence
- unary minushighest
+ unary plushighest
* multiplicationmiddle
/ division middle
% remainder middle
+ addition low
- subtractionlow

Some operators have equal precedence. For example, plus + and minus - have the same precedence.

The unary minus and unary plus operators are used as part of a negative or a positive number. For example, -23 means negative twenty-three and +23 means positive twenty-three. More on this later.

When the operands (the numbers) are both integers, then these operators do integer arithmetic. If one or both operand is floating point, then these operators do floating point arithmetic. This is especially important to remember with division, because the result of integer division is an integer. For example, 5/2 is 2 (not 2.5) and 5/10 is 0 (not 0.5). More on this later.


QUESTION 22:

What is the value of the following expressions? In each expression, do the highest precedence operator first.

Expression 16 - 12 / 4 2 + 6 / 2 8 + 4*2 8+4 * 2 12/2 - 3 6/8 + 2
Value?