What is Type Casting in Java?
Type casting means converting one data type into another data type.
Java automatically converts smaller data types into larger data types, but converting larger types into smaller types must be done manually.
Types of Type Casting
Java supports two types of casting:
- Widening Casting (Automatic Casting)
- Narrowing Casting (Manual Casting)
Widening Casting
Widening casting converts a smaller data type into a larger data type automatically.
| From | To |
|---|---|
| byte | short |
| short | int |
| int | long |
| long | float |
| float | double |
Example of Widening Casting
public class Main {
public static void main(String[] args) {
int number = 100;
double value = number;
System.out.println(number);
System.out.println(value);
}
}
Output
100 100.0
Java automatically converts int into double.
Narrowing Casting
Narrowing casting converts larger data types into smaller data types manually.
This process may cause data loss.
Example of Narrowing Casting
public class Main {
public static void main(String[] args) {
double price = 99.99;
int number = (int) price;
System.out.println(price);
System.out.println(number);
}
}
Output
99.99 99
Decimal value is lost after converting double into int.
Type Casting Syntax
Automatic Casting
largerType variable = smallerTypeVariable;
Manual Casting
smallerType variable = (smallerType) largerTypeVariable;
byte to int Example
byte age = 25; int value = age; System.out.println(value);
int to double Example
int marks = 90; double total = marks; System.out.println(total);
double to int Example
double salary = 45678.99; int amount = (int) salary; System.out.println(amount);
Character Type Casting
Characters can also be converted into integers.
char letter = 'A'; int value = letter; System.out.println(value);
Output
65
Java stores characters using Unicode values.
int to char Example
int number = 66; char letter = (char) number; System.out.println(letter);
Output
B
Data Loss in Narrowing Casting
Narrowing casting may lose information if larger value does not fit into smaller type.
int number = 130; byte value = (byte) number; System.out.println(value);
Output
-126
Real Life Example
double billAmount = 999.99; int finalAmount = (int) billAmount; System.out.println(finalAmount);
Complete Java Type Casting Program
public class Main {
public static void main(String[] args) {
int age = 25;
double value = age;
System.out.println(value);
double marks = 89.5;
int total = (int) marks;
System.out.println(total);
char letter = 'A';
int unicode = letter;
System.out.println(unicode);
}
}
Output
25.0 89 65
Advantages of Type Casting
- Helps convert compatible data types
- Useful in calculations
- Improves flexibility in programming
- Supports memory optimization
Common Errors
- Forgetting manual casting
- Loss of decimal values
- Overflow in narrowing casting
- Using incompatible data types
Best Practices
- Use widening casting whenever possible
- Avoid unnecessary narrowing casting
- Check for data loss carefully
- Use meaningful variable names
Conclusion
Type casting is an important concept in Java programming. It helps developers convert data types for calculations, memory management, and program flexibility.