Page 144 - ComputerScience_Class_11
P. 144
9 float f = 34.6f;
10 double d = 12.12;
11 double output = (d * c) + (f - i)* (s * d);
12 System.out.println("Result of the above expression (d * c) + (f - i)* (s
* d) = "+output);
13 }
14 }
The output of the preceding program is as follows:
BlueJ: Terminal Window - Java
Options
Result of the above expression (d * c) + (f - i)* (s * d) = -7461612.405234375
Since, double is the largest data type, the output we get is in double format.
6.5.2 Explicit Type Conversion
The concept of explicit data type conversion is used to assign a value of a larger data type to a smaller data type. This
process is known as narrowing. It is the reverse state of implicit data conversion. It requires the user’s intervention.
Before assigning the incompatible data type, we need to write down the data type within the brackets.
Example 1:
double d = 4.56;
int n = (int)d;
System.out.println("Result :" + n);
Output: Result : 4
Example 2:
short i;
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
Program 3 Write a program to demonstrate explicit type conversion.
1 class explicit_conversion
2 {
3 public static void main(String[] args)
4 {
142 Touchpad Computer Science (Ver. 3.0)-XI

