Home » Java Tutorial » Type Casting in Java with Examples

Type Casting in Java with Examples

Type casting in Java is the way to assign primitive data stored in one variable to another variable. If the two types are compatible, then Java will perform the conversion automatically. For example, it is always possible to assign an int value to a long variable. However, not all types are compatible, and thus, not all type conversions are implicitly allowed. For instance, there is no conversion defined from double to byte. Fortunately, it is still possible to obtain a conversion between incompatible types. To do so, you must use a cast, which performs an explicit conversion between incompatible types.

Types of type casting in Java

There are two types of typecasting in Java.

  1. Widening type casting (automatic).
  2. Narrowing type casting (manual).

Let’s look at both types of type casting one by one.

1. Widening type casting.

When one type of data is assigned to another type of variable, an automatic type conversion will take place if the following two conditions are satisfied.

  • The two types are compatible.
  • The destination type is larger than the source type.

When these two conditions are met, a widening conversion takes place. For example, the int type is always large enough to hold all valid byte values, so no explicit cast statement is required.

The numeric types, including integer and floating-point types, are compatible with each other for widening conversions. However, the numeric types are not compatible with char or boolean. Also, char and boolean are not compatible with each other. As mentioned earlier, Java also performs an automatic type conversion when storing a literal integer constant into variables of type byte, short, or long. Let’s understand the examples of type casting and type conversion in Java.

Example 1:

Type casting integer value into float and double.

public class typecasting {

    public static void main(String[] args) {
        int a = 10;
        System.out.println("Value of a in Integer format : "+a);
        float b = a; // storing integer into float
        System.out.println("Value of b in float format : "+b);
        double c = b; // storing float into double
        System.out.println("Value of c in double : "+c);


Value of a in Integer format : 10
Value of b in float format : 10.0
Value of c in double : 10.0

Example 2

Type casting character value into an integer format will print the character’s ASCII value.

public class typecasting {
    public static void main(String[] args) {
       char a = 'a';
       int b = a;



2. Narrowing type casting

Narrowing conversion will not be performed automatically, because a byte is smaller than an int. This kind of conversion is sometimes called a narrowing conversion since you are explicitly making the value narrower so that it will fit into the target type.

To create a conversion between two incompatible types, you must use a cast. A cast is simply an explicit type of conversion.


(target_data_type) variable or value;

Here, (target_data_type) specifies the desired type to convert the specified value.

Example 1

In this example, we store double typed data in a variable, and convert into long as well as int.

public class type casting {
    public static void main(String[] args) {
        double a = 9654323456.1343;
        int b = (int) a; //type casting
        System.out.println("Integer value of a : "+b);  
        long c = (long) a; // type casting
        System.out.println("Long value of a : "+c);


Integer value of a : 2147483647
Long value of a : 9654323456

As we can see, the output of int data is showing something wrong. The reason behind it is that integer data type can not store 9654323456. The range of int data type can store -2147483647 to 2147483647. To store numbers greater than 2147483647 we need a bigger data type.

Example 2

Explicit type casting in Java of an integer stored in the string into the integer, float and double format.

public class typecasting {
    public static void main(String[] args) {
        String a = "2343";
        int b = Integer.parseInt(a); //type casting
        System.out.println("Integer value of a : "+b); 
        String c = "1233.245";
        double d = Double.parseDouble(c); // type casting
        System.out.println("Double value of a : "+c);
        String e = "935874082";
        long f = Long.parseLong(e); // type casting
        System.out.println("Long value of a : "+f);



Integer value of a : 2343
Double value of a : 1233.245
Long value of a : 935874082

We can retrieve the numeric value from the string using parsers. Every data type has their parsers which helps to extract the data from a string.

Do you know?
1. Static Keyword in Java
2. Final Keyword in Java
3. Palindrome in Java
4. Wrapper class in Java
5. This Keyword in Java
6. Bubble Sort in Java
7. Armstrong Number in Java
8. Method overloading in java
9. Prime Number Program in Java
10. Type Casting In Java

Pin It on Pinterest