Introduction
A significant part of programming involves playing around with data. While performing some tasks, situations might arise where the data type has to be changed from one type to another. For example, while calculating the area of a circle using Pi, we get a decimal number, and that needs to be converted to integer if we desire to obtain the answer in integer format.

Since Java is strongly typed, one often needs to convert from one data type to another. The programmer or the compiler can easily do this by using type casting and type conversion concepts.
In Java, there are thirteen types of type Conversions. You can explore more about them by referring to the Java Type Conversion section in the Official Oracle Java Documentation. This blog will dive deep into the concept of type conversion and type casting in Java in detail, along with some examples.
Before learning Type Conversion and Type Casting in Java, you should know about Java Data Types. So check out the blog on Java Data Types and Identifiers to get a better understanding.
What is Type Conversion?
Type conversion is a process in which the data type is automatically converted into another data type. The compiler does this automatic conversion at compile time. The data type to which the conversion happens is called the destination data type, and the data type from which the conversion happens is called the source data type. If the source and destination data types are compatible, then automatic type conversion takes place.
For type conversion to take place, the destination data type must be larger than the source type. In short, the below flow chart has to be followed.

Flow chart for Type Conversion
This type of type conversion is also called Widening Type Conversion/ Implicit Conversion/ Casting Down. In this case, as the lower data types with smaller sizes are converted into higher ones with a larger size, there is no chance of data loss. This makes it safe for usage.

In type conversion, the source data type with a smaller size is converted into the destination data type with a larger size.
Before looking at an example for the type conversion type, let's see what happens when incompatible data types are given.
public class Main {
public static void main(String[] args) {
int intType = 20;
// Short is of lower data type than int
short shortType = intType;
System.out.println("intType: "+intType);
System.out.println("shortType: "+shortType);
}
}
Compile Time Error:

In the above case, short is of lower data type than int, hence a compile-time error occurs. In the next section, we will see how to solve this error.
Example for Type Conversion in Java:
Output:
intType: 20
floatType: 20.0