Basic JAVA 5
Type Casting
Assigning a value of one type to a variable of another type is known as Type Casting.
In Java, type casting is classified into two types,
1) Implicit (Widening) or Automatic type conversion
Automatic Type casting take place when,
the two types are compatible
the target type is larger than the source type
byte -> short -> int -> long -> float -> double .
Ex: int i = 100;
long l = i; //no explicit type casting required
float f = l; //no explicit type casting required
2) Explicitly or Narrowing type conversion
When you are assigning a larger type value to a variable of smaller type, then you need to perform explicit type casting.
double -> float -> long -> int -> short -> byte .
Ex: double d = 100.04;
long l = (long)d; //explicit type casting required
int i = (int)l; //explicit type casting required
Dinesh Kumar S is a 23-year-old System Administrator who enjoys playing games, listening to music and learning new technology. He is friendly and generous, but can also be very lazy and crazy.
Share this
Learn-JAVA