![]() ![]() ![]() |
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
The Java language supports various arithmetic operators for all floating-point and integer numbers. These include+
(addition),-
(subtraction),*
(multiplication),/
(division), and%
(modulo). For example, you can use this Java code to add two numbers:Or you can use the following Java code to compute the remainder that results from dividingaddThis + toThisdivideThis
bybyThis
:divideThis % byThisThis table summarizes Java's binary arithmetic operations:
Operator Use Description +
op1 + op2
Adds op1
andop2
-
op1 - op2
Subtracts op2
fromop1
*
op1 * op2
Multiplies op1
byop2
/
op1 / op2
Divides op1
byop2
%
op1 % op2
Computes the remainder of dividing op1
byop2
Note: The Java programming language extends the definition of the+
operator to include string concatenation. You'll see more about this in Strings.
Here's an example program,
ArithmeticDemo
, that defines two integers and two double-precision floating point numbers and uses the five arithmetic operators to perform different arithmetic operations. This program also uses + to concatenate strings. The arithmetic operations are shown in red:
The output from this program is:public class ArithmeticDemo { public static void main(String[] args) { //a few numbers int i = 37; int j = 42; double x = 27.475; double y = 7.22; //adding numbers System.out.println("Adding..."); System.out.println(" i + j = " + (i + j)); System.out.println(" x + y = " + (x + y)); //subtracting numbers System.out.println("Subtracting..."); System.out.println(" i - j = " + (i - j)); System.out.println(" x - y = " + (x - y)); //multiplying numbers System.out.println("Multiplying..."); System.out.println(" i * j = " + (i * j)); System.out.println(" x * y = " + (x * y)); //dividing numbers System.out.println("Dividing..."); System.out.println(" i / j = " + (i / j)); System.out.println(" x / y = " + (x / y)); //computing the remainder resulting from dividing numbers System.out.println("Computing the remainder..."); System.out.println(" i % j = " + (i % j)); System.out.println(" x % y = " + (x % y)); //mixing types System.out.println("Mixing types..."); System.out.println(" j + y = " + (j + y)); System.out.println(" i * x = " + (i * x)); } }Note that when an integer and a floating point number are used as operands to a single arithmetic operation, the result is floating point. The integer is implicitly converted to a floating point number before the operation takes place. The following table summarizes the data type returned by the arithmetic operators based on the data type of the operands. The necessary conversions take place before the operation is performed:Adding... i + j = 79 x + y = 34.695 Subtracting... i - j = -5 x - y = 20.255 Multiplying... i * j = 1554 x * y = 198.37 Dividing... i / j = 0 x / y = 3.8054 Computing the remainder... i % j = 37 x % y = 5.815 Mixing types... j + y = 49.22 i * x = 1016.58#include tables/conversions.mem4
In addition to the binary forms of
+
and-
, each of these operatators have unary versions that perform the following operations:
Operator Use Description +
+op
Promotes op
toint
if it's abyte
,short
, orchar
-
-op
Arithmetically negates op
There also are two short cut arithmetic operators,
++
which increments its operand by 1, and--
which decrements its operand by 1. Either++
or--
can appear before (prefix) or after (postfix) its operand. The prefix version,++op
/--op
, evaluates to the value of the operand after the increment/decrement operation. The postfix version,op++
/op--
, evaluates the value of the operand before the increment/decrement operation.Recall the
SortDemo
program.
Let's look at how thepublic class SortDemo { public static void main(String[] args) { int[] arrayOfInts = { 32, 87, 3, 589, 12, 1076, 2000, 8, 622, 127 }; 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] + " "); } System.out.println(); } }SortDemo
program uses--
to control the outer of its two nested sorting loops. Here's the statement that controls the outer loop:The return value of the operationfor (int i = arrayOfInts.length; --i >= 0; ) { ... }--i
is compared to 0 to determine whether to terminate the loop. Using the prefix version of--
means that the last iteration of this loop occurs wheni
is equal to 0. If we changed the code to use the postfix version of--
, the last iteration of this loop occurs wheni
is equal to -1, which is incorrect for this program.The other two loops in the program use the the postfix version of
++
. In both cases, the version used doesn't really matter because the value returned by the operator isn't used for anything. When the return value of one of these shortcup operations isn't used for anything, convention prefers the postfix version.The short cut increment/decrement operators are summarized in the following table:
Operator Use Description ++
op++
Increments op
by 1; evaluates to the value of op before it was incremented++
++op
Increments op
by 1; evaluates to the value of op after it was incremented--
op--
Decrements op
by 1; evaluates to the value of op before it was decremented--
--op
Decrements op
by 1; evaluates to the value of op after it was decremented
![]() ![]() ![]() |
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |