Its the process of allocating the data value from one type to another. while assigning the value there may
be a chance that two data type is not compatible.
Type casting is classified into two types:
(1) Implicit type casting (widening or automatic type casting).
(2) Explicit type casting (narrowing or manually type casting)
Implicit type casting :-
-> when two types are compatible then this type casting is take place.
-> this type casting is done automatically (done by compiler).
-> This type casting is done from smaller type two larger type.
byte -> short -> char -> int -> long -> float -> double
eg:-
int i = 100;
long l = i;
float f = l;
System.out.println("int value :"+i);
System.out.println("long value :"+l);
System.out.println("float value :"+f);
output:-
int value :100
long value :100
float value :100.0
Explicit type casting :-
-> when two types are not compatible then explicit type casting take place.
-> This type casting is done manually (done by programmer).
-> This type casting is done from larger type to smaller type.
double -> float -> long -> int -> char -> short -> byte
eg:-
double d = 100.04;
long l = (long)d;
int i = (int)l;
System.out.println("double value :"+d);
System.out.println("long value :"+l);
System.out.println("int value :"+i);
output:-
double value :100.04
long value :100
int value :100
Comments