|
This chapter covers the concepts of type conversions and type casting extensively. The fifth SCJP objective, specifically sections 5.1 and 5.4 requires you to know all about type changes in Java. In this chapter, we learned about the automatic type change (conversion) and explicit type change (casting). We also learned about the various checks Java performs at compile and runtime to ensure appropriate type for each value. Here is a brief summary of concepts we learned.
q Types in Java
§ There are two kinds of types: primitives and object references.
q Type conversion and type casting
There are two kinds of type changes, conversion and casting.
An implicit type change performed by Java is called as type
conversion.
An explicit type change performed by programmer with a cast
operator is called as type casting.
q Contexts define various situations in which type change can occur.
§ Contexts of type change for primitives
o Assignment
o Method call
o Numeric/Arithmetic operations
o String concatenation
§ Contexts of type change for references
o Assignment
o Method call
o String concatenation
q Conversion of primitives in assignment and method call
§ The rules of primitive conversion during method call and assignment are same.
§ Rules of primitive conversion in assignment and method call.
o Primitive type boolean may not be converted to any other data type.
o Non-boolean primitive data types can be converted to non-boolean only if it is widening conversion.
q Widening conversions in primitives
q Narrowing conversion and integral literals
§ When Java automatically converts a wider data type into narrower data type, it is called as narrowing conversion.
§ Java performs this type of conversion only in case of integral literal and only when it is assigned to a narrower data type than int (byte, char or short) and when their value is within the range of that datatype.
§ Narrowing conversions are not performed for floating-point literals.
q Conversion in primitives during the numeric/arithmetic operations
In numeric operations, sometimes it is necessary to promote
the operands to a common type so that the operations can be performed
meaningfully.
Arithmetic Promotions for unary operators
o For unary +, - and ~ , if the operand is of type byte, short or char, it is promoted to int. If the operand is of any other type, it is not changed.
o For ++ and -- operators , there is no conversion.
Arithmetic Promotions for binary operators
o If one of the operand is double, other is promoted to double before operation.
o Else, if one of the operand is float, other is promoted to float before operation
o Else, if one of the operand is long, other is promoted to long before operation.
o Else, both operands are promoted to int before operation.
o Please note that both the operands must be of non-boolean primitive type.
q Casting of primitive data types
Casting rules of primitives are checked at compile-time.
§ Primitive casting may result in loss of information but it does not throw any exception at runtime.
§ Value as well as sign of the primitive value might change due to casting.
o Any non-boolean to any non-boolean is allowed.
o boolean to non-boolean casting is not allowed.
o Non-boolean to boolean casting is not allowed.
q Object reference Conversion.
Object references are of three kinds: class-type, interface-type
and array-type.
Reference conversion rules are same for assignments as well as
method call.
Rules of reference conversion
o A class-type can be converted to another class-type, if the new class-type is its superclass.
o A class-type can be converted to an interface-type, if the class implements that interface.
o An interface-type can be converted to new interface-type, if the new interface-type is its superinterface.
o In case of array of primitives, an array-type can be converted only to the java.lang.Object class-type or the Serializable and Cloneable interface-types.
o In case of array of object references, an array-type can be converted to the java.lang.Object class-type or the Serializable and Cloneable interface-types.
o An array of object references can be converted into a new array of object references provided the elements of that array are assignment-compatible (convertible) with the elements of new array.
q Widening conversions in object references
q Passing arguments to a method
Always a copy of argument is passed to a method.
If a primitive argument is passed to a method and if the method
modifies it, the original value does not change.
If an object reference is passed to a method then that method
can modify the original object though the copy of passed reference.
q Object reference Casting.
Java has two sets of rules for object reference casting,
compile-time and run-time checks.
If your code does not comply with compile-time rules, it will not
compile.
If your code complies with compile-time rules but not with
runtime rules then it will compile but will fail at runtime and throw ClassCastException.
q Compile time rules for reference casting
q Runtime rules for reference casting
If you are casting one class-type to a new class-type, the new
class-type must be a superclass of it.
If you are casting one class-type to an interface-type, then that
interface must be the implemented by the class.