|
Conversion, Casting and Promotion
|
Conversion
and casting
[More in ebook]
|
|
|
Conversion
Contexts
Following conversion contexts define different
situations in which implicit conversions
occur.
- Assignment conversion
- Method invocation conversion
- Numeric/Arithmetic promotion
- String conversion
Casting specifies a special
situation in which explicit conversion takes
place.
[More in ebook]
Rules for
conversion of primitives in assignment.
-
boolean may
not be converted to any other data type.
- Non-boolean primitive data types can be
converted to non-boolean only if it is widening
conversion.

[More in ebook]
Rules for
conversion of primitives during the method
call.
The rules are same as those of assignment.
-
boolean may not
be converted to any other data type.
- Non-boolean primitive data types can be
converted to non-boolean only if it is widening
conversion.
[More in ebook]
Rules for
conversion of primitives during the
numeric/arithmetic operations.
Arithmetic Promotions for unary operators.
- For ++ and -- operators , there is no
conversion.
- For unary +, - and ~ , widening conversion
happens as variables of type byte, short or char are promoted to
int.
Arithmetic Promotions for binary
operators.
- If one of the operand is double, other is
promoted to double before operation.
- Else, if one of the operand is float, other
is promoted to float before operation
- Else, if one of the operand is long, other
is promoted to long before operation.
- Else, both operands are promoted to int
before operation.
Please note that both the operands must be
of non-boolean primitive type.
|
[More in ebook]
|
Casting of
primitive data types
Rules
- Any non-boolean to any non-boolean is
allowed.
- boolean to non-boolean casting is NOT
allowed.
- Non-boolean to boolean casting is NOT
allowed.
[More in ebook]
|
|
Object
reference Conversion.
Object reference Conversion rules for assignment
and method calls are same.
Rule
Any object reference can be converted into other
object type provided it is a widening
conversion.
Widening reference conversion.
OldType anObjectRef = new OldType();
NewType anotherObjectRef = anObjectRef; //conversion.
Object reference conversion specified above is
valid ...
- If OldType is an interface type then
NewType must be the super interface of the
OldType.
- If OldType is a class, and
- If NewType is also a class, it must be
the super class of OldType.
- Alternatively, if NewType is an
interface, OldType must implement it.
- If OldType is an array, NewType can be of
type Object or of type Cloneable, Serializable.
Alternatively, NewType can be an array of
compatible type.
|
[More in ebook]
|
Object
reference Casting.
Object reference casting rules are different for
compile time and runtime.
Compile-Time Rules
(Less strict)
|
Runtime Rules
(More strict)
|
- If OldType and NewType are classes,
then any one of them must be the subclass
of the other.
- If both are arrays , they must be
arrays of object references and data
types of array elements must be
compatible.(You cannot cast an array of one primitive type to an array of another primitiive type).
- You can always cast an interface to a
non-final object.
|
- NewType must be the super class of
OldType.
- If NewType is an interface, OldType
must implement it.
|
[More in ebook]
|
|