Page 113 - computer science (868) class 11
P. 113
float f = 7.14f;
i = (short)(f/4.3);
System.out.println("Result : "+ i);
Output: Result : 1
Here, target-type specifies the desired type to convert the specified value to.
The following diagram depicts the order of the data types from larger to smaller.
double float long int short byte
Example 1:
class explicit_conversion
{
public static void main()
{
short sd;
int id;
long ld = 2571;
double dd = 323.14;
System.out.print("Converting long to int: ");
id = (int)ld;
System.out.println("long ld = " +ld+ " int id = " +id);
System.out.println("---------------------------------------------");
System.out.print("Converting double to short: ");
sd = (short)dd;
System.out.println("double dd = " +dd+ " short sd = " +sd);
}
}
The output of the preceding program is as follows:
Note: The boolean data type is not compatible with any other primitive data types. So, it cannot be
converted to any other type or vice versa neither by implicit nor explicit data type conversion.
5.6 WRAPPER CLASS
Wrapper classes are in-built classes that contain primitive data types. These classes are used to convert primitive data
types into objects and vice-versa with the help of their methods. Every wrapper class has various methods. Thus, it
provides a way to use the primitive data types (short, double, boolean, etc.) as objects. Every primitive data type is
attached to its wrapper class. For example, the float data type is attached to the Float class, the double data type
is attached to the Double class, and so on.
111
Primitive Values, Wrapper Classes, Types and Casting 111

