the same as the sign of the first operand. While the modulo operator is typically
used with integer operands, it also works for floating-point values. For exam‐
ple, 4.3%2.1 evaluates to 0.1. When operating with integers, trying to compute
a value modulo zero causes an ArithmeticException . When working with
floating-point values, anything modulo 0.0 evaluates to NaN , as does infinity
Unary minus ( - )
When the - operator is used as a unary operator—that is, before a single
operand—it performs unary negation. In other words, it converts a positive
value to an equivalently negative value, and vice versa.
String Concatenation Operator
In addition to adding numbers, the + operator (and the related += operator) also
concatenates, or joins, strings. If either of the operands to + is a string, the operator
converts the other operand to a string. For example:
// Prints "Quotient: 2.3333333"
System . out . println ( "Quotient: " + 7 / 3.0f );
As a result, you must be careful to put any addition expressions in parentheses
when combining them with string concatenation. If you do not, the addition opera‐
tor is interpreted as a concatenation operator.
The Java interpreter has built-in string conversions for all primitive types. An object
is converted to a string by invoking its toString() method. Some classes define
custom toString() methods so that objects of that class can easily be converted to
strings in this way. An array is converted to a string by invoking the built-in
toString() method, which, unfortunately, does not return a useful string represen‐
tation of the array contents.
Increment and Decrement Operators
The ++ operator increments its single operand, which must be a variable, an element
of an array, or a field of an object, by 1. The behavior of this operator depends on its
position relative to the operand. When used before the operand, where it is known
as the pre-increment operator, it increments the operand and evaluates to the incre‐
mented value of that operand. When used after the operand, where it is known as
the post-increment operator, it increments its operand, but evaluates to the value of
that operand before it was incremented.
For example, the following code sets both i and j to 2:
i = 1 ;
j = ++ i ;
But these lines set i to 2 and j to 1:
i = 1 ;
j = i ++;