In this article we learn the what is type casting in java and types of type casting.
Define Type Casting in Java
Type Conversion or Type Casting is the process of converting a data type into another data type.
Types of Java Type Casting
There are two types of java type casting:
1. Widening Type Casting or Implicit Type Casting
2. Narrowing Type Casting or Explicit Type Casting
1. Widening Type Casting | Implicit Type Casting in Java
Converting a lower data type into a higher one is called widening type casting. It is also known as implicit conversion or casting down. It is done automatically. It is safe because there is no chance to lose data.
byte -> short -> char -> int -> long -> float -> double.
2. Narrowing Type Casting | Explicit Type Casting in Java
Converting a higher data type into a lower one is called narrowing type casting. It is also known as explicit conversion or casting up. It is done manually by the programmer. If we do not perform casting, then the compiler reports a compile-time error.
double -> float -> long -> int -> char -> short -> byte.