Java Reference
In-Depth Information
-
Subtraction (also unary minus)
*
Multiplication
/
Division
%
Modulus
++
Increment
- -
Decrement
The operators
+
,
-
,
*
, and
/
all work the same way in Java as they do in any other com-
puter language (or algebra, for that matter). These can be applied to any built-in numeric
data type. They can also be used on objects of type
char
.
Although the actions of arithmetic operators are well known to all readers, a few special
situations warrant some explanation. First, remember that when
/
is applied to an integer,
any remainder will be truncated; for example, 10/3 will equal 3 in integer division. You can
obtain the remainder of this division by using the modulus operator
%
. It works in Java the
way it does in other languages: it yields the remainder of an integer division. For example,
10 % 3 is 1. In Java, the
%
can be applied to both integer and floating-point types. Thus,
10.0 % 3.0 is also 1. The following program demonstrates the modulus operator.
The output from the program is shown here: