Type Casting
What is Type Casting?
The method that converts a value from one data type to another is called type casting. Java’s type casting feature allows you to change a variable’s data type, allowing you to use it in an expression or assignment that requires a different data type. In Java, we can cast both references and primitive data types.
Type casting helps to compute expressions containing variables of different data types. By typecasting, no information is lost on the overall magnitude of the numeric value
Example:
public class CastingExample { public static void main(String[] args) { int x=123; double y=22.34; x=(int)y; System.out.println("y= "+y); System.out.println("x= "+x); int h = 3; short w = 5; System.out.println("h= "+h); System.out.println("w= "+w); h = w; w = (short)h; System.out.println("h= "+h); System.out.println("w= "+w); } }
Output:
y= 22.34
x= 22
h= 3
w= 5
h= 5
w= 5
let’s explain the program.
Explanation:
- Initially, the code declares and initializes an int variable x with the value 123 and a double variable y with the value 22.34.
- It then performs casting by converting the double value y to an int and assigns the result to x. This is explicit casting since (int) is used to indicate the conversion.
- The code prints the values of y and x to see the difference after casting. You’ll notice that the value of y remains the same, but the value of x changes to 22, which is the integer part of the original y.
- Next, the code declares and initializes an int variable h with the value 3 and a short variable w with the value 5.
- It then assigns the value of w to h without explicit casting. This is implicit casting because h is an int, and it can safely hold the value of w, which is a short.
- Finally, the code performs explicit casting to convert the int value h back to a short and stores it in w. The values of h and w are printed again, showing that they are both equal to 5 after the explicit casting.
Overall, this code demonstrates both explicit and implicit casting in Java, where explicit casting requires a manual type conversion, and implicit casting is done automatically when converting to a compatible type.
Kinds of Type Casting
There are different types of type casting in Java. But two major types of type casting that we use in Java are:
- Implicit casting or widening casting
- Explicit casting or narrow casting
Implicit casting or widening casting:
Implicit type casting in Java, also known as widening or automatic type conversion, occurs when the compiler automatically converts a value from one data type to another compatible data type without the need for explicit casting by the programmer.
The general rule for implicit casting is that it can only occur when converting to a larger data type or a data type with a wider range, where no data loss can occur.
Here are some common examples of implicit casting:
Widening numeric conversions: Converting from a smaller data type to a larger one:
Example:
public class CastingExample { public static void main(String[] args) { int intValue = 123; long longValue = intValue; System.out.println("Int value before casting: " + intValue); System.out.println("Long value after casting: " + longValue); } }
Output:
Int value before casting: 123
Long value after casting: 123
Widening with byte, short, and char: Converting between byte, short, and char, they are promoted to int before any operation:
Example:
public class CastingExample { public static void main(String[] args) { byte b = 5; short s = b; // Implicit casting from byte to short char c = 'A'; int i = c; // Implicit casting from char to int System.out.println("byte value before casting: " + b); System.out.println("short value after casting: " + s); System.out.println("char value before casting: " + c); System.out.println("int value after casting: " + i); } }
Output:
byte value before casting: 5
short value after casting: 5
char value after casting: A
int value after casting: 65
Promoting floating-point types: Converting from a lower precision floating-point type to a higher precision one:
Example:
public class CastingExample { public static void main(String[] args) { float floatValue = 4.15f; double doubleValue = floatValue; // Implicit casting from float to double System.out.println("float value before casting: " + floatValue); System.out.println("double value after casting: " + doubleValue); } }
Output:
float value before casting: 4.15
double value after casting: 4.150000095367432
It’s important to note that implicit casting is always done by the compiler, and it doesn’t require any explicit syntax from the programmer.
Explicit casting or narrow casting:
Explicit type casting in Java, also known as narrowing, is the process of manually converting a variable from one data type to another data type with a smaller range or precision. In Java, explicit casting is when you manually convert a variable from one data type to another by specifying the target data type in parentheses. This is done to handle situations where there might be a risk of losing data during the conversion.
The general syntax for explicit type casting is:
targetDataType variableName = (targetDataType) originalValue;
Here are some examples of explicit type casting:
Converting from a floating-point type to an integral type:
Example:
public class CastingExample { public static void main(String[] args) { double doubleValue = 3.14; int intValue = (int) doubleValue; // Explicit casting from double to int System.out.println("double value before casting: " + doubleValue); System.out.println("int value after casting: " + intValue); } }
Output:
double value before casting: 3.14
int value after casting: 3
Converting from a larger data type to a smaller one:
public class CastingExample { public static void main(String[] args) { long longValue = 4379000000L; int intValue = (int) longValue; // Explicit casting from long to int System.out.println("long value before casting: " + longValue); System.out.println("int value after casting: " + intValue); } }
Output:
long value before casting: 4379000000
int value after casting: 84032704
Converting between char and numeric types:
public class CastingExample { public static void main(String[] args) { char ch = 'A'; int asciiValue = (int) ch; // Explicit casting from char to int System.out.println("char value before casting: " + ch); System.out.println("int value after casting: " + asciiValue); } }
Output:
char value before casting: A
int value after casting: 65
In this example, we explicitly cast the char value ch to an int to get its ASCII value.
It’s important to exercise caution when using explicit type casting because it may lead to data loss or unexpected results if the value exceeds the representable range of the target data type. Always ensure that the value being cast is within the valid range of the target data type before performing explicit casting