Java Type Casting
Type casting in Java is the process of converting a variable from one data type to another. It's useful when you need to perform operations that require matching data types or when you want to optimize memory usage.
Key Topics
1. Widening Casting (Implicit)
Widening casting is done automatically when passing a smaller size type to a larger size type. It's also known as implicit casting because it doesn't require any special syntax.
Conversion Order: byte
→ short
→ char
→ int
→ long
→ float
→ double
Example
public class WideningCasting {
public static void main(String[] args) {
int myInt = 9;
double myDouble = myInt; // Automatic casting: int to double
System.out.println("Integer value: " + myInt);
System.out.println("Double value: " + myDouble);
}
}
Output:
Double value: 9.0
Explanation: The integer value 9
is automatically cast to a double value 9.0
without any explicit code.
2. Narrowing Casting (Explicit)
Narrowing casting must be done manually by placing the type in parentheses in front of the value. It's also known as explicit casting and may result in data loss.
Conversion Order: double
→ float
→ long
→ int
→ char
→ short
→ byte
Example
public class NarrowingCasting {
public static void main(String[] args) {
double myDouble = 9.78;
int myInt = (int) myDouble; // Manual casting: double to int
System.out.println("Double value: " + myDouble);
System.out.println("Integer value: " + myInt);
}
}
Output:
Integer value: 9
Explanation: The double value 9.78
is explicitly cast to an integer, resulting in 9
. The fractional part is lost.
3. Type Casting Examples
3.1 Widening Casting Example
public class WideningCastingExample {
public static void main(String[] args) {
byte byteValue = 42;
short shortValue = byteValue;
char charValue = (char) shortValue;
int intValue = charValue;
long longValue = intValue;
float floatValue = longValue;
double doubleValue = floatValue;
System.out.println("byte: " + byteValue);
System.out.println("short: " + shortValue);
System.out.println("char: " + charValue);
System.out.println("int: " + intValue);
System.out.println("long: " + longValue);
System.out.println("float: " + floatValue);
System.out.println("double: " + doubleValue);
}
}
Output:
short: 42
char: *
int: 42
long: 42
float: 42.0
double: 42.0
3.2 Narrowing Casting Example
public class NarrowingCastingExample {
public static void main(String[] args) {
double doubleValue = 123.456;
float floatValue = (float) doubleValue;
long longValue = (long) floatValue;
int intValue = (int) longValue;
char charValue = (char) intValue;
short shortValue = (short) charValue;
byte byteValue = (byte) shortValue;
System.out.println("double: " + doubleValue);
System.out.println("float: " + floatValue);
System.out.println("long: " + longValue);
System.out.println("int: " + intValue);
System.out.println("char: " + (int) charValue);
System.out.println("short: " + shortValue);
System.out.println("byte: " + byteValue);
}
}
Output:
float: 123.456
long: 123
int: 123
char: 123
short: 123
byte: 123
Key Takeaways
- Type casting allows conversion between compatible data types.
- Widening casting is automatic and safe as it doesn't lose information.
- Narrowing casting must be done manually and can lead to data loss.
- Always be cautious when narrowing types to avoid unexpected results.