Java Reference
In-Depth Information
cast, conditional, equality, logical, member access, method call, multiplicative, object
creation, relational, shift, and unary minus/plus.
Additive Operators
The additive operators consist of addition (
+
), subtraction (
-
), postdecrement (
--
),
postincrement (
++
), predecrement (
--
), preincrement (
++
), and string concatenation
(
+
).Additionreturnsthesumofitsoperands(e.g.,
6+4
returns10),subtractionreturns
thedifferencebetweenitsoperands(e.g.,
6-4
returns2and
4-6
returns2),postdecre-
mentsubtractsonefromitsvariableoperandandreturnsthevariable'spriorvalue(e.g.,
x--
), postincrement adds one to its variable operand and returns the variable's prior
value(e.g.,
x++
),predecrementsubtractsonefromitsvariableoperandandreturnsthe
variable'snewvalue(e.g.,
--x
),preincrementaddsonetoitsvariableoperandandre-
turnsthevariable'snewvalue(e.g.,
++x
),andstringconcatenationmergesitsstringop-
erands and returns the merged string (e.g.,
"A"+"B"
returns
"AB"
).
Theaddition,subtraction,postdecrement,postincrement,predecrement,andpreincre-
ment operators can yield values that overflow or underflow the limits of the resulting
value'stype.Forexample,addingtwolargepositive32-bitintegervaluescanproducea
valuethatcannotberepresentedasa32-bitintegervalue.Theresultissaidtooverflow.
Java does not detect overflows and underflows.
Java provides a special widening conversion rule for use with string operands and
the string concatenation operator. If either operand is not a string, the operand is first
converted to a string prior to string concatenation. For example, when presented with
"A"+5
,thecompilergeneratescodethatfirstconverts
5
to
"5"
andthenperformsthe
string concatenation operation, resulting in
"A5"
.