In the previous topic, we learned about type conversion, where Java can automatically convert a value from one data type to another in certain situations.
But what happens when Java cannot perform the conversion automatically? In that case, we can tell Java explicitly which data type we want. This is called type casting.
In simple words, type casting means explicitly converting a value from one data type to another.
Why Do We Need Type Casting?
Let's say we have a double value:
double price = 99.99;
Now suppose we want to store it in an int:
int amount = price;
This will give us an error because double can store decimal values, while int can only store whole numbers. Java doesn't want to automatically throw away the decimal part.
If we are sure that we want to do this conversion, we can explicitly tell Java:
int amount = (int) price;
Here, (int) tells Java to convert price into an integer.
Syntax of Type Casting
The basic syntax is:
dataType variable = (dataType) value;
For example:
double number = 25.75;
int result = (int) number;
The (int) is called the cast operator. It tells Java that we want the double value to be converted into an int.
If we print result:
System.out.println(result);
The output will be:
25
Notice that .75 is removed. This is important because type casting can cause loss of data.
Widening and Narrowing Casting
Type casting is commonly discussed in two forms: widening casting and narrowing casting.
Widening means converting a smaller type into a larger type. Java can usually do this automatically.
For example:
int number = 10;
double value = number;
Narrowing means converting a larger type into a smaller type. This may cause data loss, so we usually need to perform it explicitly.
For example:
double number = 10.75;
int value = (int) number;
Here, the decimal part is lost.
You can think of it like pouring water from a small glass into a large bottle versus pouring water from a large bottle into a small glass. The first one is easy, but in the second case, some water might be lost.
Casting double to int
This is one of the most common examples of type casting.
double price = 99.99;
int amount = (int) price;
System.out.println(amount);
Output:
99
Java does not round the number. It simply removes the decimal part.
For example:
double number = 25.99;
int result = (int) number;
System.out.println(result);
Output:
25
Even though 25.99 is closer to 26, casting it to int gives us 25.
Casting int to double
We can also explicitly cast an int to a double:
int number = 25;
double result = (double) number;
System.out.println(result);
Output:
25.0
However, this particular conversion does not normally require explicit casting because Java can automatically convert an int to a double.
So this is enough:
int number = 25;
double result = number;
Casting char to int
We can also convert a character into an integer.
char letter = 'A';
int value = (int) letter;
System.out.println(value);
Output:
65
The number comes from the character's Unicode value.
We can also convert an integer into a character:
int value = 66;
char letter = (char) value;
System.out.println(letter);
Output:
B
This works because 66 corresponds to the character B in Unicode.
A Complete Example
Let's put type casting into a small program:
class Main {
public static void main(String[] args) {
double price = 499.99;
int amount = (int) price;
System.out.println("Original price: " + price);
System.out.println("Converted price: " + amount);
}
}
Output:
Original price: 499.99
Converted price: 499
The original double value remains 499.99, but the new int value contains only 499.
Be Careful About Data Loss
This is the most important thing to remember about narrowing type casting.
When you convert a value into a type that cannot store all of the original information, some data may be lost.
For example:
double number = 15.87;
int result = (int) number;
The result is:
15
The .87 is gone.
So before using type casting, always ask yourself whether losing some information is acceptable.
Type casting gives us more control, but that control also means we need to use it carefully.