HOME HTML EDITOR C JAVA PHP

Java Type Casting: Converting Data Types

Type casting is when you assign a value of one primitive data type to another type. In Java, there are two types of casting: Widening and Narrowing.

Why Cast? Sometimes you need to perform operations between different types (like dividing an int by a float). Type casting tells Java how to handle these conversions safely.

1. Widening Casting (Automatically)

Widening casting happens when you pass a smaller type to a larger type size. It is done **automatically** by the Java compiler because there is no risk of losing data.

The hierarchy of widening casting is as follows:

byte -> short -> char -> int -> long -> float -> double
int myInt = 9; double myDouble = myInt; // Automatic casting: int to double System.out.println(myInt); // Outputs 9 System.out.println(myDouble); // Outputs 9.0

In the example above, the integer 9 is automatically converted to the double 9.0 because a double (8 bytes) is larger than an int (4 bytes).

2. Narrowing Casting (Manually)

Narrowing casting happens when you pass a larger type to a smaller size type. This must be done **manually** by placing the type in parentheses () in front of the value.

Note: Narrowing casting can result in "Data Loss" because the smaller type might not be able to hold the full value of the larger type.

double -> float -> long -> int -> char -> short -> byte
double myDouble = 9.78d; int myInt = (int) myDouble; // Manual casting: double to int System.out.println(myDouble); // Outputs 9.78 System.out.println(myInt); // Outputs 9 (Fractional part .78 is lost)

3. Practical Application: Percentage Calculation

Type casting is very useful when you have integer data but you need a precise decimal result.

int maxScore = 500; int userScore = 423; /* Without casting, the result would be 0.0 because int/int performs integer division */ float percentage = (float) userScore / maxScore * 100.0f; System.out.println("User Percentage: " + percentage + "%");

4. Understanding Data Overflow

What happens if you cast a number that is too big for the target type? Java will perform a "wrap-around". For example, a byte can only hold values up to 127. If you cast 130 to a byte, you will get a unexpected negative number.

The Binary Logic: In narrowing casting, Java simply chops off the extra bits. This is why you should always be careful when casting down to byte or short.

5. Type Promotion Rules

When you perform arithmetic operations, Java automatically promotes the operands to a common type:

6. Comparison Table

Feature Widening (Implicit) Narrowing (Explicit)
Conversion Small to Large Large to Small
Effort Automatic by Compiler Manual by Programmer
Data Loss No risk of loss High risk of loss
Memory Grows Shrinks

Module Summary

Type casting is an essential tool for every Java developer. It allows you to control how data flows between different types of variables. Now that you know how to convert data, it's time to learn how to manipulate it! In the next chapter, we will explore Java Operators.

Next: Java Operators →