Page 143 - ComputerScience_Class_11
P. 143
There are two types of conversions in Java, which are as follows:
• Implicit Type Conversion
• Explicit Type Conversion
6.5.1 Implicit Type Conversion
Implicit type conversion takes place when the target data type is larger than the source type, i.e., when we assign the
value of a smaller data type to a larger data type.
byte
short
char int float
long double
In implicit type conversion, the resulting data type is automatically determined by the compiler. This process is also known as
widening conversion. An important point to note is that no loss of data occurs during implicit conversion
From the widening order, we can observe that a byte value can be converted to short, which can further be converted
to int, long, float or double as required. Similarly, the char data type can be promoted to int, long, float or double.
When a char value is converted to int, the numeric value of the character according to the ASCII/Unicode representation
is obtained.
For example, let us see the following code snippet:
char ch = 'A';
int n = ch;
System.out.println(ch+ ": "+n);
Output:
A: 65 // 65 is the ASCII value of A
Program 2 Write a program to demonstrate implicit type conversion.
1 class implicit_datatype
2 {
3 public static void main(String[] args)
4 {
5 byte b = 12;
6 char c = 'A';
7 short s = 124;
8 int i = 5000;
Primitive Values, Wrapper Classes, Types and Casting 141

