What is Type Casting in Java?
In Java, type casting refers to converting one data type into another. It is an essential concept in programming that ensures compatibility between different data types. Type casting is particularly useful when working with variables of different types in the same program. Java supports both automatic type casting (widening) and manual type casting (narrowing).
Types of Type Casting in Java
- Implicit Type Casting (Widening)
Implicit type casting occurs automatically when a smaller data type is converted into a larger one. It is safe because there is no loss of data or precision.Example of Widening:- Converting int to long
- Converting float to double
- Explicit Type Casting (Narrowing)
Explicit type casting is a manual conversion of a larger data type into a smaller one. This requires a cast operator and may result in data loss if the value exceeds the target type’s range.
Implicit Type Casting (Widening)
Widening automatically happens when a smaller data type is assigned to a larger data type.
- Example: byte → short → int → long → float → double
Example Code for Widening:
public class WideningExample {
public static void main(String[] args) {
int number = 100;
double result = number; // Implicit casting from int to double
System.out.println("Widened Value: " + result);
}
}
Output:
Widened Value: 100.0
Explicit Type Casting (Narrowing)
Narrowing requires explicit instructions from the programmer. The cast operator (type) is used to convert larger data types into smaller ones.
- Example: double → float → long → int → short → byte
Example Code for Narrowing:
public class NarrowingExample {
public static void main(String[] args) {
double price = 99.99;
int discountedPrice = (int) price; // Explicit casting from double to int
System.out.println("Discounted Price: " + discountedPrice);
}
}
Output:
Discounted Price: 99
Type Casting for Compatibility
Sometimes, type casting is necessary for working with mixed data types. Consider the following example where calculations involve both int and double types.
Example:
public class MixedTypeCasting {
public static void main(String[] args) {
int num = 5;
double divisor = 2.0;
double result = num / divisor; // Automatic widening to double
System.out.println("Result: " + result);
}
}
Output:
Result: 2.5
Important Notes on Type Casting
Data Loss in Narrowing: When narrowing, values exceeding the target type’s range can lead to data loss.
Example:
public class DataLossExample {
public static void main(String[] args) {
int largeNumber = 130;
byte smallNumber = (byte) largeNumber; // Overflow occurs
System.out.println("Converted Value: " + smallNumber);
}
}
Output:
Converted Value: -126
Precision Loss in Floating-Point Conversion: Casting from double to float can cause precision loss.
Casting Characters: Java allows type casting between char and numeric types.
Example:
public class CharCasting {
public static void main(String[] args) {
char letter = 'A';
int ascii = (int) letter; // Casting char to int
System.out.println("ASCII Value of A: " + ascii);
}
}
Output:
ASCII Value of A: 65
Wrapper Classes and Type Casting
Java provides wrapper classes that allow conversions between primitive types and objects using methods like valueOf() and intValue().
Example:
public class WrapperCasting {
public static void main(String[] args) {
Integer num = 100; // Wrapper object
int primitiveNum = num.intValue(); // Converting to primitive
System.out.println("Primitive Value: " + primitiveNum);
}
}
Output:
Primitive Value: 100
Type Casting in Arrays
When working with arrays, type casting can convert elements from one type to another if needed.
Example:
public class ArrayCasting {
public static void main(String[] args) {
int[] numbers = {1, 2, 3};
double[] doubleNumbers = new double[numbers.length];
for (int i = 0; i < numbers.length; i++) {
doubleNumbers[i] = numbers[i]; // Implicit casting
}
for (double d : doubleNumbers) {
System.out.println(d);
}
}
}
Output:
1.0
2.0
3.0