Introduction
Java is a statically typed programming language in which the data types and their variables are known at compile time. Once a variable is declared of a specific data type, it cannot be changed anywhere else in the later parts of the program.

Data types, as we all know, can be primitive or non-primitive.
- Primitive data types can hold only single values and have no special properties. Boolean, short, int, float, double, char, long and byte are the eight primitive data types in Java.
- Non-primitive data types store the memory address of a variable value and not the value itself in the memory. Hence, they are also known as reference data types. Non-primitive data types can store multiple values under the same name and have their own set of properties. Arrays, strings, class, objects and interfaces are examples of non-primitive data types in Java.
In this article, we will explore the topic type conversion in Java with examples in detail.
What is Type Conversion in Java
Programs perform operations and calculations which may or may not involve variables of different data types. Compatible data types do not cause problems during execution. However, incompatible ones need to be cast or converted explicitly. Each data type is assigned a certain memory space to store its value, so if variables of different types are involved, finding the data type of the result is uncertain. Thus, types conversion is of two types.
- Widening or Automatic Type Conversion
- Narrowing or Explicit Conversion
It is important to note here that conversion between two primitive and two non-primitive data types is only possible. We cannot typecast a primitive to a non-primitive data type and vice versa. Implicit and explicit type conversions are performed based on a precedence structure where data types are arranged in a particular order.
1. Widening or Automatic Type Conversion
Automatic or implicit type conversion is done by the compiler internally. Converting a lower data type into a higher one is known as widening. If two variables involved in an operation are of the same data type, the result is also similar. If variables of different but compatible data types are involved, then implicit conversion is automatic.
Implicit conversion follows the given order:
For example, char variables can be typecasted to int, float can be typecasted to double and so on.
public class Main
{
public static void main(String[] args)
{
char ch = 'a';
int a = ch;
System.out.println(a);
long a = 20;
float b = 24.62f;
double x = a*b;
System.out.println(x);
}
}
Output:
97
472.4000244140625
The char type is automatically converted to int. Variables a and b, which are of long and float data type, respectively, are multiplied to give a value stored in a double type variable. So, long and double are implicitly cast to double.
Note that if we try to typecast a float variable to a double without adding a float literal, it will result in an error stating lossy conversion. This is because variables of double data type have more precision than float. The error is a warning from the compiler indicating reduced precision.
2. Narrowing or Explicit Conversion
Narrowing or Explicit conversion involves casting a higher data type into a lower one. This type of conversion is not done automatically, and the desired data type needs to be explicitly mentioned within ( ). Data types need not be compatible with each other for explicit typecasting.
Explicit conversion uses the following order:
public class Main
{
public static void main(String[] args)
{
int a = 102;
char ch = (int) a;
System.out.println(ch);
double x = 328.288006786;
long y = (long) x/20;
System.out.println(x);
}
}
Output:
f
16
In the above example, an int variable is explicitly converted to char, which is of lower order. Similarly, result y, by default, will be of double type but has been converted to long data type. Hence, y stores the integer answer of the division operation.