PREV NEXT
- In Java, there are two kinds of data types – primitives and references. Primitives include int, float, long, double etc. References can be of type classes, interfaces, arrays.
- A value can change its type either implicitly or explicitly.
Implicit and Explicit changes:
There are situations in which the system itself changes the type of an expression implicitly based on some requirement.
Example:
int num1 = 4;
double num2; double d = num1 / num2; //num1 is also converted to double |
This kind of conversion is called automatic type conversion.
Type Casting:
In some cases changes do not occur on its own, the programmer needs to specify the type explicitly. This is called casting. However, conversion and casting can be performed, only if they are compatible with the some rules defined.
Syntax for casting:
Identifier2 = (type) identifier1; |
Example:
num 1= (int) num2; |
Widening vs. Narrowing Conversion:
Basically, primitives are converted using either one of the two forms.
Widening Conversion:
Changes a value to a type with a larger range. This will not result in any data loss.
Example, converting from int to long.
Narrowing Conversion:
Changes a value to a type with a shorter range. Some data may get lost.
Example, converting from double to float.
Conversion and Casting of Primitives:
There are three situations in which conversion of primitive type takes place.
- Assignment
- Arithmetic Promotion
- Method call
Assignment Conversion:
Assignment conversion occurs when a variable of one type is assigned to another type. Only widening conversions can take place through assignment.
Example:
double x;
int y = 50; x = y; |
Arithmetic promotion:
Arithmetic promotions occur in arithmetic expressions where the operator requires the operands to be meaningful while performing the operation.
Example:
int count =7;
double sum = 24; double average = sum / count; //count is also converted to double byte b = 5; byte++; //byte is converted to int, then ++ is performed |
Method call:
Conversions occur during a method call, when an argument of a type is passed to a method which expects a different type.
Example:
float f = 1.234f
double d = Math.cos(f); //method cos() expects double and hence f is converted to double |
Casting of primitives:
Casting represents mentioning of the type explicitly to make a conversion. Since widening conversions are implicit by default, casting is useful for performing narrowing conversions. Narrowing conversion has the risk of losing information.
Example:
int a = 5;
short b = (short) a; |
Conversion and Casting of Reference Types:
References are of type class, interface, and array. Reference type conversion cannot have arithmetic promotion since object references cannot be arithmetic operands.
Assignment conversion:
Assignment conversion happens when a reference type is assigned to a value of different type.
The rules for object reference conversion can be stated as follows:
- An interface type may only be converted to an interface type or to Object. If the new type is an interface, it must be a super interface of the old type.
- A class type may be converted to a class type or to an interface type. If converting to a class type, the new type must be a super class of the old type. If converting to an interface type, the old class must implement the interface.
- An array may be converted to the class Object, to the interface Cloneable or Serializable, or to an array. Only an array of object reference types may be converted to an array, and the old element type must be convertible to the new element type.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
class Bird { … } class Peacock extends Bird { … } public class TypeExample { public static void main(String[] argv) { Bird b1 = new Bird(); Peacock p1 = new Peacock(); b1 = p1; //allowed p1 = b1; //not allowed } } |
Method invocation conversion:
Conversion of reference type can also occur when a reference type is passed to a method which accepts argument of a different type. Conversion to a super class is permitted but conversion to a subclass type is not permitted.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class Example { public static void main(String[] argv) { Peacock p1 = new Peacock(); myMethod(p1); } public static void myMethod(Bird b) { } } |
Casting of reference types:
- Casting of reference type is similar to casting of primitives.
- Rules for casting are same for that of assignment and method invocation conversion.
PREV NEXT