Page 72 - CA_Blue( J )_Class9
P. 72
You will get the following output:
4.7 TYPE CONVERSION
The process of changing the data type of one variable to another data type is known as type conversion. It is also
known as type casting. Type conversion is necessary in situations when a method or an expression returns one
type of value and you want to store it in a variable of different data type.
The casting is performed by keeping the target data type in parentheses to the left of the value which is being
converted.
Syntax:
datatype variable = (datatype)variable_to_be_converted;
Let’s understand with given example:
int number = 5;
double decimal = (double)number;
Here, “number” is a variable of int data type and it is converted to double data type and stored in the “decimal”
variable.
There are two types of conversions in Java: Implicit conversion and Explicit conversion.
4.7.1 Implicit Type Conversion
Implicit Type conversion takes place when the two data types are compatible and the target data type is larger
than the source data type. This process is also known as widening because the smaller data type is "widened"
to fit into a larger data type. The Java compiler handles this conversion automatically without requiring explicit
casting.
For example, when assigning the value of a smaller data type (such as int) to a larger data type (such as double),
the implicit conversion is performed automatically by the Java compiler.
byte short int long float double
Program 14 Write a program to demonstrate implicit conversion.
1 class implicit_conversion
2 {
3 public static void main()
4 {
70 Touchpad Computer Applications-IX

