Java supports all of your standard arithmetic operators for performing basic math on Java variables and/or literals. This programming tutorial will take a closer look at both Java’s binary other unary math operators, resulting data types, as well as operator precedence rules.
Reading: Java Primitive Data Types
Binary arithmetic operators
Java’s binary arithmetic operators are employed to perform operations that involve two numbers. Java supports a total of 5 binary arithmetic operators, which are applicable to all floating-point and integer numbers. They are:
- + (addition)
- – (subtraction)
- * (multiplication)
- / (division)
- % (modulo)
Unless you skipped grade 3 math, you are probably familiar with the first four arithmetic operators. That last one modulo – is used more rarely than the others; it computes the remainder of dividing one number by another. Hence, if we were to divide 3 by 2the remainder would be 1. In code that would be expressed as int remainder = 3 % 2.
Here is a code example showing a class that shows the use of the above operators on a variety of integer and double number combinations:
public class BinaryOperatorsExample { public static void main(String[] args) { //declare a few numbers int int1 = 3; int int2 = 5; double double1 = 9.99; double double2 = 6.66; System.out.println(“Variable values:”); System.out.println(” int1 = ” + int1); System.out.println(” int2 = ” + int2); System.out.println(” double1 = ” + double1); System.out.println(” double2 = ” + double2); //adding numbers System.out.println(“Addition:”); System.out.println(” int1 + int2 = ” + (int1 + int2)); System.out.println(” double1 + double2 = ” + (double1 + double2)); //subtracting numbers System.out.println(“Subtraction:”); System.out.println(” int1 – int2 = ” + (int1 – int2)); System.out.println(” double1 – double2 = ” + (double1 – double2)); //multiplying numbers System.out.println(“Multiplication:”); System.out.println(” int1 * int2 = ” + (int1 * int2)); System.out.println(” double1 * double2 = ” + (double1 * double2)); //dividing numbers System.out.println(“Division:”); System.out.println(” int1 / int2 = ” + (int1 / int2)); System.out.println(” double1 / double2 = ” + (double1 / double2)); //computing the remainder after division System.out.println(“Remainders:”); System.out.println(” int1 % int2 = ” + (int1 % int2)); System.out.println(” double1 % double2 = ” + (double1 % double2)); //mixing types System.out.println(“Mixing types:”); System.out.println(” int2 + double2 = ” + (int2 + double2)); System.out.println(” int1 * double1 = ” + (int1 * double1)); } }
Compiling and executing the above program produces the following output:
Variable values: int1 = 3 int2 = 5 double1 = 9.99 double2 = 6.66 Addition: int1 + int2 = 8 double1 + double2 = 16.65 Subtraction: int1 – int2 = -2 double1 – double2 = 3.33 Multiplication: int1 * int2 = 15 double1 * double2 = 66.5334 Division: int1 / int2 = 0 double1 / double2 = 1.5 Remainders: int1 % int2 = 3 double1 % double2 = 3.33 Mixing types: int2 + double2 = 11.66 int1 * double1 = 29.97
Reading: Top Online Courses to Learn Java
Result Types of Arithmetic Operations in Java
Mixing two different data types within a single arithmetic operation will cause one of the operands to be converted to the other’s type before the operation occurs. The common type is then maintained in the result. For example, mixing an integer with a floating-point number produces a floating point result as the integer is implicitly converted to a floating-point type. Here is a summary of the data type returned by the arithmetic operators, based on the data type of the operands:
- internal: Neither operand is a float or a double (integer arithmetic); neither operand is a long.
- long: Neither operand is a float or a double (integer arithmetic); at least one operand is a long.
- double: At least one operand is a double.
- float: At least one operand is a float; neither operand is a double.
Expression Evaluation Rules in Java
You may be surprised to learn that what we developers think of as operator precedence actually pertains to three different rules! They are operator precedence, operator association, and order of operand evaluation. Java relies on all three rules for evaluating expressions, so let’s look at each of them.
Operator Precedence in Java
As you may already be aware, operator precedence governs how operands are grouped with operators. With regards to the arithmetic operators, *, ?and % have a higher precedence than + other –. Hence, 1+2*3 is treated as 1+(2*3)whereas 1*2+3 is treated as (1 * 2) + 3. Developers can use parentheses to override the built-in operator precedence rules; for example: (1 + 2) * 3.
Java Operator Associativity
Since *, ?and % all share equal precedence, as do + other –, this begs the question: what happens when an expression has two operators with the same precedence? In that instance, the operators and operands are grouped according to their associativity. The Java arithmetic operators are all left-to-right associative, so that 99/2/4 is treated as (99 / 2) / 4. Again, programmers can use parentheses to override the default operator associativity rules.
Java Order of Operand Evaluation
Associativity and precedence determine in which order Java groups operands and operators, but it does not determine in which order the operands are evaluated. Luckily, in Java, this one is a no-brainer, as the operands of an operator are always evaluated left-to-right. The order of operand evaluation rule comes into play when function argument lists and subexpressions are involved. For instance, in the expression a() + b() * c(d(), e())the subexpressions are evaluated in the order a(), b(), d(), e()and c().
Unary Arithmetic Operators in Java
the + other – operators have the distinction of working in both a binary other unary context. Here is how each operator function in unary mode in Java:
- +: eg, + oprepresents the operand as a positive value
- –: eg, – oprepresents the operand as a negative value
As seen in the following example, applying the + operator on a positive number, or applying the – operator on a negative number, has no effect, which is useful if you do not know a number’s sign beforehand:
int a = 24; int b = -24; System.out.println(+a); // 24 System.out.println(+b); // -24 System.out.println(-a); // -24 System.out.println(-b); // 24
Java also supports the shortcut arithmetic operators ++ other —which increment and decrement their operands by 1 respectively. thesis unary operators can be placed before (prefix) or after (post fix) their operands, thereby affecting evaluation order. The prefix version, ++op/-opevaluates to the value of the operand nach the increment/decrement operation, whereas the postfix version, op++/op–evaluates to the value of the operand before the increment/decrement operation.
Programmers will often see the increment/decrement operators in for loops, such as these, which sort an array of integers:
public class IncrementorDecrementorSortExample { public static void main(String[] args) { final int[] arrayOfInts = { 9, 65, 3, 400, 12, 1024, 2000, 33, 733 }; for (int i = arrayOfInts.length; –i >= 0; ) { for (int j = 0; j < i; j++) { if (arrayOfInts[j] > arrayOfInts[j+1]) { int temp = arrayOfInts[j]; arrayOfInts[j] = arrayOfInts[j+1]; arrayOfInts[j+1] = temp; } } } for (int i = 0; i < arrayOfInts.length; i++) { System.out.print(arrayOfInts[i] + " "); } } } // outputs: 3 9 12 33 65 400 733 1024 2000
Final Thoughts on Java Math Operators
In this programming tutorial, we learned about Java’s binary other unary arithmetic operators. These are best suited for performing basic math operations on Java variables. For more complex calculations, Java also provides the Java math class, which includes a number of methods such as minutes(), Max(), round(), random()and many others.
read more Java programming tutorials and guides to software development.