CodeGym /Java Blog /Java Types /Java Type Casting
Artem Divertitto
Senior Android Developer at United Tech

Java Type Casting

Published in the Java Types group

What is Type Casting in Java?

A data type is a predefined set of values that specify the type of values that can be stored in it along with the operation that can be carried out on them.
The Java Type Casting is a process by which one data type is converted to another.
It can be both implicit and explicit. Implicit typecasting also known as automatic typecasting is done by the compiler. Explicit typecasting is done manually by the programmer in the code.

Why is Type Casting Required?

Java has different primitive data types which require different spaces in memory. This can cause compatibility issues while assigning a value of one data type to another. If the datatypes are already compatible, typecasting is done automatically by the compiler. Thus, typecasting solves a major compatibility problem when dealing with different data types in our program.

Types of Java Type Casting

There are two types of type casting in Java.
  1. Widening Type Casting — Also known as Implicit or Automatic Type Casting
  2. Narrowing Type Casting — Also known as Explicit or Manual Type Casting

Widening Type Casting

Widening typecasting, as the name suggests, refers to the widening of a smaller data type to a larger data type. We perform this typecasting when we want to convert a small type to a large type. The data types must be compatible with each other. There is no implicit conversion from numeric to char or boolean type. In Java, char and boolean types are non-compatible.
byte -> short -> char -> int -> long -> float -> double
This type of casting is done automatically by the compiler without any loss of information. It does not require any external trigger by the programmer.


//Automatic type conversion
public class WideningExample {
   public static void main(String[] args) {

       int i = 100;
       System.out.println("int value: " + i);

       // int to long type
       long l = i;
       System.out.println("int to long value: " + l);

       // long to float type
       float f = l;
       System.out.println("int to float value: " + f);

       byte b = 1;
       System.out.println("byte value: " + b);

       // byte to int type
       i = b;
       System.out.println("byte to int value: " + i);

       char c = 'a';
       System.out.println("char value: " + c);

       // char to int type
       i = c;

       // prints the ASCII value of the given character
       // ASCII value of 'a' = 97
       System.out.println("char to int value: " + i);


int value: 100 int to long value: 100 int to float value: 100.0 byte value: 1 byte to int value: 1 char value: a char to int value: 97


In the code above, we have shown widening typecasting which is done by the compiler automatically. First of all, we assigned values to an int, byte, and char. We then assigned the int values to a long and float, both of which are larger than int. We also assigned the byte and char values to int. Both byte and char are smaller data types than int, thus, these conversions were implicit.

Narrowing Type Casting

Narrowing typecasting, as the name suggests, refers to the narrowing of a larger data type to a smaller data type. We perform this typecasting when we want to convert a large type to a small type.
double -> float -> long -> int -> char -> short -> byte
For this type of casting, we override the Java’s default conversion by specifying our own conversion. To achieve this, we write the variable or value that needs to be typecasted preceded by the target data type in parentheses ‘()’. However, this type of casting may result in a possible loss of precision.


//Manual Type Conversion
public class NarrowingExample {
   public static void main(String[] arg) {

       // double data type
       double d = 97.04;
       // Print statements
       System.out.println("double value: " + d);

       // Narrowing type casting from double to long
       // implicitly writing the target data type in () followed by initial data type
       long l = (long) d;

       // fractional part lost - loss of precision
       System.out.println("long value: " + l);

       // Narrowing type casting from double to int
       // implicitly writing the target data type in () followed by initial data type
       int i = (int) l;

       // fractional part lost - loss of precision
       System.out.println("int value: " + i);

       // Narrowing type casting from double to int
       // implicitly writing the target data type in () followed by initial data type
       char c = (char) i;

       // displaying character corresponding to the ASCII value of 100
       System.out.println("char value: " + c);


double value: 97.04 long value: 97 int value: 97 char value: a


Narrowing typecasting needs to be done explicitly by the programmer using a standard syntax. In the program above, we have started with a double value which is larger than the long and int data types. We typecasted this double to long and int by using parentheses with the desired target data types. We have also manually typecasted ‘int’ to ‘char’. Java Type Casting - 1


By the end of this post, we hope you have got yourself familiarized with the Java Type Casting in detail. You have learned the two types of typecasting in java. You have also learned how to manually cast the non-compatible data types using Narrowing typecasting. You can try other combinations of data types with different values on your own to understand it in more depth. Keep practising for a deeper command of the concept. Till then, keep growing and keep shining!