Java Reference
In-Depth Information
thelatterassignmentbecausenoinformationislostwhenJavaconvertsfromatypewith
a smaller set of values to a type with a wider set of values.
Javasupportsthefollowingprimitivetypeconversionsviawideningconversionrules:
• Byteintegertoshortinteger,integer,longinteger,floating-point,ordoublepre-
cision floating-point
• Short integer to integer, long integer, floating-point, or double precision
floating-point
• Character to integer, long integer, floating-point, or double precision floating-
point
• Integer to long integer, floating-point, or double precision floating-point
• Long integer to floating-point or double precision floating-point
• Floating-point to double precision floating-point
Note
When converting from a smaller integer to a larger integer, Java copies the
smaller integer's sign bit into the extra bits of the larger integer.
Chapter2
discussesthewideningconversionrulesforperformingtypeconversionsin
the context of user-defined and array types.
Compound Expressions
A
compound expression
isasequenceofsimpleexpressionsandoperators,wherean
op-
erator
(asequenceofinstructionssymbolicallyrepresentedinsourcecode)transforms
its
operand
expressionvalue(s)intoanothervalue.Forexample,
-6
isacompoundex-
pression consisting of operator
-
and integer literal
6
as its operand. This expression
transforms
6
intoitsnegativeequivalent.Similarly,
x+5
isacompoundexpressioncon-
sisting ofvariable name
x
,integer literal
5
,andoperator
+
sandwiched between these
operands. Variable
x
's value is fetched and added to
5
when this expression is evalu-
ated. The sum becomes the value of the expression.
Note
If
x
's type is byte integer or short integer, this variable's value is widened
to an integer. However, if
x
's type is long integer, floating-point, or double precision
floating-point,
5
is widened to the appropriate type. The addition operation is per-
formed after the widening conversion takes place.
Javasuppliesawidevarietyofoperatorsthatareclassifiedbythenumberofoperands
theytake.A
unary operator
takesonlyoneoperand(unaryminus[
-
]isanexample),a
binary operator
takestwooperands(addition[
+
]isanexample),andJava'ssingle
tern-
ary operator
(conditional [
?:
]) takes three operands.