In this article we are going to cover an easy topics of arithmetic operators and expressions in Java. Arithmetic is the basic calculation of numbers using operators (like addition and subtraction). An arithmetic expression is a mathematical formula that can be evaluated to numeric value and consists of operands (numbers and variables) and operators.
An arithmetic expression can be very simple and contains only a number or can be very complex that contains many operands and operations. The following shows a lists of examples of expressions. We are assigning the result of the expressions to a variable named
result; however, the right hand side of the equation is what we mean by expression.
Operators and Order of Operation
The following table lists Java’s arithmetic operators along with the precedence of operation. Java has a very powerful expression evaluation system and follows the traditional order of evaluation: expression are evaluated from left to right; expressions in parentheses are evaluated first; and multiplications and divisions comes before additions and subtractions.
|%||modulus (division remainder)|
Operator with lower precedence value is evaluated first.
Note that Java does not have the exponentiation operator; however, exponentiation can be accomplished using the
Math.pow() method of Java’s standard library.
Most of the operators are self-explanatory. Here we give a few examples of the operators (and the result of evaluation).
In addition to the operator presented in the previous, Java also supports increment and decrement operators (shown in table below). These operators are used to add or subtract an integer variable by 1.
|++||increment||Add 1 to a variable|
|- -||decrement||substract 1 from a variable|
These operators can be applied before (prefix) or after (postfix) a variable. Examples are below.
We can apply a shorthand assignment operator to a variable. These are form by combing an operator with the assignment operator. The effect of this is that the operation is performed to the variable, then the result is assigned to the same variable. The following code shows examples.
About Data Types
Every operand of a operation has a type. For example when we have expression
2 + 3, both
3 has an implicit type of
int. Of course when a variable is used in an expression, the type of the variable is the operand’s type. Implicit types depend on the literal.
When both operands are the same type, there is no type conversion; and the result data type is also the same type. When operands are different type, Java implicitly converts one operand to the other type, then performs the operation. This is called numeric promotion, where a narrower type is promoted to a larger type.
Sometime this gives unexpected result. Consider the following example. We expect the value of
result to be
0.5; however, if you run the program, you would see that the program outputs
0.0! This is because both operands are integers, so the operation is actually an integer division, which produces 0, then converted to a
float by the assignment operator.
There is also the issue of data conversion discussed in the article Data Types. The following code gives the error
"incompatible types: double found where float required".
The reason for this error is that
4.0 are implicitly of type
double. The result, also a
double, is then to be converted to a
float. This conversion is a narrowing conversion, which causes an error.
To remedy the error, it is better to avoid narrowing conversion, such that declare the variable as
double. The other approach is to force the result to a
float using a cast, as shown below.