Java Type Casting

Java Type Casting Tutorial


Type casting is a way of converting data from one data type to another data type. This process of data conversion is also known as type conversion or type coercion.

In Java, we can cast both reference and primitive data types. By using casting, data can not be changed but only the data type is changed.

Note: type casting is not possible for a Boolean data type.

There are 13 types of conversion in Java. In this article, we will only look at 2 major types:

  1. Implicit casting.
  2. Explicit casting.

To read about the other types of casting, refer to this documentation.

Implicit casting takes place under two conditions:

  • Compatibility of the data types. For example, data types such as numeric are compatible with other numeric data types, but they are not compatible with boolean, char, or string data types. In the same way as string is not compatible with a boolean data type.