15.15
Cast Expressions
EXPRESSIONS
15.15 Cast Expressions
My days among the dead are passed;
Around me I behold,
Where'er these casual eyes are cast,
The mighty minds of old . . .
Robert Southey (1774 1843),
Occasional Pieces
, xviii
A cast expression converts, at run time, a value of one numeric type to a similar
value of another numeric type; or confirms, at compile time, that the type of an
expression is
boolean
; or checks, at run time, that a reference value refers to an
object whose class is compatible with a specified reference type.
CastExpression:
(
PrimitiveType Dims
opt
)
UnaryExpression
(
ReferenceType
)
UnaryExpressionNotPlusMinus
See 15.14 for a discussion of the distinction between
UnaryExpression
and
UnaryExpressionNotPlusMinus
.
The type of a cast expression is the type whose name appears within the
parentheses. (The parentheses and the type they contain are sometimes called the
cast operator
.) The result of a cast expression is not a variable, but a value, even if
the result of the operand expression is a variable.
At run time, the operand value is converted by casting conversion ( 5.4) to the
type specified by the cast operator.
Not all casts are permitted by the Java language. Some casts result in an error
at compile time. For example, a primitive value may not be cast to a reference
type. Some casts can be proven, at compile time, always to be correct at run time.
For example, it is always correct to convert a value of a class type to the type of its
superclass; such a cast should require no special action at run time. Finally, some
casts cannot be proven to be either always correct or always incorrect at compile
time. Such casts require a test at run time. A
ClassCastException
is thrown if a
cast is found at run time to be impermissible.
350
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved