Curriculum
Course: Complete Java Programming Course
Login

Curriculum

Complete Java Programming Course

Text lesson

Type Conversion and Casting in Java

[post-views]

 

 

 

In this lesson, you will learn

  • Type Conversion and Casting
  • Implicit Type Conversion
  • Explicit Type Conversion

 

Type Conversion and Casting

Type Conversion and Casting in Java are fundamental concepts used to transform data from one type to another.

In Java, there are two types of conversions: implicit and explicit.

 

1. Implicit Conversion (Automatic Type Conversion)

  • Occurs when the Java compiler automatically converts one data type to another.
  • This typically happens when storing a smaller-size data type into a larger-size data type, a process known as “widening conversion” or “upcasting”.
  • For example, converting an int to a long, or a float to a double.
  • This conversion is safe and doesn’t lead to data loss.

 

Example 1: ImplicitTypeConversion

public class ImplicitTypeConversionExample {
    public static void main(String[] args) {
        // Starting with a byte
        byte myByte = 100;
        char myChar='A';
        
        // Implicit conversion from byte to short
        short myShort = myByte;
        
        // Implicit conversion from short to int
        int myInt = myShort;
        
        // Implicit conversion from char to int
        int myInt1 = myChar;
        
        // Implicit conversion from int to long
        long myLong = myInt;
        
        // Implicit conversion from long to float
        float myFloat = myLong;
        
        // Finally, implicit conversion from float to double
        double myDouble = myFloat;
        
        // Displaying all the converted values
        System.out.println("Byte value: " + myByte);
        System.out.println("Short value (from byte): " + myShort);
        System.out.println("Int value (from short): " + myInt);
        System.out.println("Int value (from char): " + myInt1);
        System.out.println("Long value (from int): " + myLong);
        System.out.println("Float value (from long): " + myFloat);
        System.out.println("Double value (from float): " + myDouble);
    }
}

 

Output

Byte value: 100
Short value (from byte): 100
Int value (from short): 100
Int value (from char): 65
Long value (from int): 100
Float value (from long): 100.0
Double value (from float): 100.0

 

Example 2: Arithmetic Operations

int a = 5;
double b = 6.2;

// a is automatically converted to double
double result = a + b; 

 

Here, a is an int and b is a double. In the expression a + b, a is automatically converted to double before the addition since double is a wider type than int. The result of the operation is of type double.

 

2. Explicit Conversion (Type Casting)

  • Explicit Conversion, also known as Type Casting, is where you manually convert one data type into another.
  • This is necessary when you want to convert a larger-size data type into a smaller-size data type, called “narrowing conversion” or “downcasting“.
  • For example, converting a double to an int.
  • This type of conversion can lead to data loss, so it must be done carefully.

Syntax of Type Casting

The syntax for type casting in Java is:

(targetType) variableToBeCasted;

Here, targetType is the type you are casting to, and variableToBeCasted is the variable you want to cast.

 

Example 1: Explicit Type Conversion

Below is an example that demonstrates explicit casting among different types in Java, from larger to smaller types, covering double, float, long, int, short, and byte.

public class TypeCasting{
    public static void main(String[] args) {
        // Starting with a double
        double myDouble = 129.99;
        
        // Casting double to float
        float myFloat = (float) myDouble;
        
        // Casting float to long
        long myLong = (long) myFloat;
        
        // Casting long to int
        int myInt = (int) myLong;
        
        // Casting int to short
        short myShort = (short) myInt;
        
        // Finally, casting short to byte
        byte myByte = (byte) myShort;
        
        // Displaying all the converted values
        System.out.println("Double value: " + myDouble);
        System.out.println("Float value (from double): " + myFloat);
        System.out.println("Long value (from float): " + myLong);
        System.out.println("Int value (from long): " + myInt);
        System.out.println("Short value (from int): " + myShort);
        System.out.println("Byte value (from short): " + myByte);
        //Outputs -127, overflow occurs due to byte range (-128 to 127)
    }
}

 

Output

Double value: 129.99
Float value (from double): 129.99
Long value (from float): 129
Int value (from long): 129
Short value (from int): 129
Byte value (from short): -127

 

Example 2: Char to Byte Conversion

char myChar = 'A'; // 'A' has an ASCII value of 65
byte myByte = (byte) myChar; // Explicit casting is required

System.out.println("Original char value: " + myChar); 
// Outputs A

System.out.println("Converted byte value: " + myByte); 
// Outputs 65, ASCII value of 'A'

 

Considerations in Type Casting

1. Data Loss: When performing narrowing casting, there is a risk of data loss. For example, casting a double to a int might lose the fractional part.

2. Precision Loss: Even when casting between numeric types, precision can be lost (e.g., float to int).

3. ClassCastException: In object casting, a ClassCastException can occur if you try to cast an object to a subclass of which it is not an instance.

 


 

 


 

End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

5.0
5.0 out of 5 stars (based on 3 reviews)
Excellent
Very good
Average
Poor
Terrible

 

 

12 February 2026

Good

4 April 2025

The content is detailed and awesome. The topic wise flow is good. Never loose interest while reading it.

27 February 2025

good

 

 

Submit a Review