Type Casting

Estimated reading: 3 minutes 24 views

Type casting is the process of converting one data type into another. In Java, casting can be implicit (automatically done by the compiler) or explicit (manually done by the programmer). Understanding type casting is important for handling different data types and ensuring compatibility in operations.

1. Implicit Casting (Widening)

Implicit casting, also known as widening, occurs when a smaller data type is automatically converted to a larger data type. This happens when there’s no risk of data loss. The Java compiler performs this casting automatically.

Example:

				
					int num = 10;      // int (4 bytes)
double result = num;  // Implicit casting from int to double
System.out.println(result); // Output: 10.0

				
			

In this example, the int value is automatically converted to a double because double has a larger range and can accommodate the int value.

2. Explicit Casting (Narrowing)

Explicit casting, or narrowing, happens when you manually convert a larger data type to a smaller one. Since there’s a risk of data loss (for example, truncating a double to an int), Java requires you to use explicit syntax to tell the compiler that you are aware of the potential loss of data.

Syntax:

				
					newType variable = (newType) variableName;

				
			

Example:

				
					double num = 9.75;
int result = (int) num;  // Explicit casting from double to int
System.out.println(result); // Output: 9

				
			

Here, the double value 9.75 is explicitly cast to an int, and the fractional part is discarded.

When to Use Implicit and Explicit Casting

  • Implicit casting is safe and used when there is no risk of losing data, typically when converting from a smaller data type to a larger one (e.g., int to long, float to double).

  • Explicit casting is needed when there is a risk of losing data, such as converting from a larger type to a smaller one (e.g., double to int, long to short).

Example Code: Implicit and Explicit Casting

				
					public class TypeCastingExample {
    public static void main(String[] args) {
        // Implicit Casting (Widening)
        int intValue = 100;
        double doubleValue = intValue; // Automatically casted to double
        System.out.println("Implicit Casting: " + doubleValue); // Output: 100.0

        // Explicit Casting (Narrowing)
        double decimalValue = 9.99;
        int intValueFromDecimal = (int) decimalValue; // Manually casted to int
        System.out.println("Explicit Casting: " + intValueFromDecimal); // Output: 9
    }
}

				
			

Output:

				
					Implicit Casting: 100.0
Explicit Casting: 9

				
			

Key Points to Remember

  • Implicit casting is automatic when there’s no loss of data (smaller to larger types).
  • Explicit casting is required when data may be lost (larger to smaller types).
  • Always ensure that you are aware of the data being truncated or lost when performing explicit casting.

Leave a Comment

Share this Doc

Type Casting

Or copy link

CONTENTS