Typecasting in Java
Typecasting in Java refers to the process of converting data from one primitive data type to another. It helps when you need to store a value of one type into a variable of another type.
Types of Typecasting in Java
- Implicit typecasting / Widening / Type promotion / Up – Casting
- Explicit typecasting / Narrowing / Down-Casting
Conversion Chain:
In implicit typecasting (widening/up-casting), the conversion chain proceeds from smaller to larger data types (right to left in the diagram) and is performed automatically by the compiler. In contrast, explicit typecasting (narrowing/down-casting) requires the programmer to manually convert larger data types to smaller ones (left to right in the diagram). Note that boolean does not participate in typecasting, as shown in the diagram.
1. Implicit Typecasting (Widening Conversion)
- Performed automatically by the compiler.
- Converts smaller data types into larger data types.
- For example, byte can be converted to short, int, long, float, or double.
Example 1: Implicit Typecasting (Widening Conversion)
class ShikshaSanchar_implicitTypecasting{
public static void main(String[] args){
byte a = 45;
double b;
b = a; // Implicit conversion from byte to double
System.out.println(a);
System.out.println(b);
}
}
Output:
System.out.println(a); // 45
System.out.println(b); // 45.0
Explanation:
- Here, the value of a (type byte) is automatically promoted to double type when assigned to b.
- The compiler handles this conversion internally, so no cast operator (type) is needed.
Note: No casting operator is needed because this is a widening conversion (automatic type promotion) and there is no loss of data.
2. Explicit Typecasting (Narrowing Conversion)
- Performed manually by the programmer using the cast operator (()).
- Converts larger data types into smaller data types.
- Care must be taken because narrowing conversion can cause loss of data if the value exceeds the range of the target type.
Example 2: Explicit Typecasting (Narrowing Conversion)
class ShikshaSanchar_implicitTypecasting{
public static void main(String[] args){
double a = 45;
byte b;
b = (byte) a; // Explicit conversion from double to byte
System.out.println(a); // Output: 45.0
System.out.println(b); // Output: 45
}
}
Output:
45.0
45
Explanation:
- Here, (byte) is used to explicitly cast the double value a to a byte.
- The programmer must specify the cast because this is anarrowing conversion (larger → smaller type).
Here, (byte) is used for explicit casting because we are converting from a larger to a smaller data type.
Note: Invalid Conversion Example
class ShikshaSanchar_InvalidConversion {
public static void main(String[] args) {
double a = 45;
byte b;
b = a; // Compilation Error: incompatible types
System.out.println(a);
System.out.println(b);
}
}
Explanation:
- You cannot assign a double value directly to a byte variable without using explicit typecasting.
- This will cause a compilation error because Java does not allow narrowing conversions automatically.
- To fix this, you must explicitly cast: b = (byte) a;.
Reason:
- Narrowing conversion (larger → smaller type) must be done manually to avoid accidental loss of data.
Bonus Point: Narrowing Conversion Out of Range
Question:
In Java, when you explicitly cast a larger primitive type to a smaller type, and the value is out of the target type’s range, how is the resulting value determined?
Formula:
Result = V mod 2^N
Where:
- V = original value
- N = number of bits of the target type (byte = 8, short = 16, int = 32, long = 64)
After applying modulo, interpret the result in two’s complement for signed types.
Example 1: int → byte
int a = 130;
byte b = (byte) a;
System.out.println(b); // Output: -126
Explanation:
- byte has 8 bits, range = -128 to 127.
- Apply formula: 130 % 256 = 130 → interpret as signed: 130 - 256 = -126
Example 2: long → int
long x = 2147483648L; // Integer.MAX_VALUE + 1
int y = (int) x;
System.out.println(y); // Output: -2147483648
Explanation:
- int has 32 bits, range = -2147483648 to 2147483647.
- Apply formula: 2147483648 % 2^32 = 2147483648 → interpret as signed: 2147483648 - 2^32 = -2147483648
Key Point:
- Narrowing conversion always uses modular arithmetic.
- If the value exceeds the range of the target type, the result wraps around according to two’s complement.
Summary
- Typecasting: Converting one primitive type to another.
- Implicit (Widening / Up-Casting): Done automatically. Converts smaller → larger types (byte → int → double) without data loss. No cast operator needed.
- Explicit (Narrowing / Down-Casting): Done manually using (type). Converts larger → smaller types (double → int → byte). May cause data loss if value exceeds target range.
- Boolean: Does not participate in typecasting.
- Invalid Conversion: Assigning larger → smaller type without casting causes compilation error.
- Out-of-Range Narrowing: Result = V mod 2^N, interpreted using two’s complement (wrap-around occurs).