While writing Java programs, we sometimes need to work with values of different data types. For example, we may have an int value but need to use it as a double. Converting a value from one data type to another is called type conversion.
The good news is that Java can perform some conversions automatically. Let's understand how this works.
What is Type Conversion?
Type conversion means changing a value from one data type to another data type.
For example, suppose we have:
int number = 10;
If we want to store this value in a double, Java can automatically convert it:
int number = 10;
double value = number;
Now number contains an integer, while value contains the same value as a double.
10 (int)
↓
10.0 (double)
This kind of automatic conversion is called implicit type conversion.
Widening Conversion
When Java converts a smaller data type into a larger data type, it is called widening conversion.
For example:
int number = 25;
double value = number;
System.out.println(value);
Output:
25.0
The int value 25 is automatically converted to double because a double can represent a much wider range of numeric values.
A common numeric widening order is:
byte → short → int → long → float → double
So, for example, an int can be automatically converted to a long or double.
int age = 25;
long largeNumber = age;
double number = age;
No special syntax is required for these conversions.
Why Does Java Allow This Automatically?
Think about putting a small amount of water into a larger bottle. If the larger bottle has enough space, there is no problem.
Java follows a similar idea with widening conversion. When the destination type can safely hold the original value, Java can perform the conversion automatically.
For example:
int score = 95;
double result = score;
There is no problem storing 95 as 95.0.
Converting char to a Number
Java can also automatically convert a char into an integer type.
For example:
char letter = 'A';
int value = letter;
System.out.println(value);
Output:
65
This happens because characters have numeric values based on Unicode.
You don't need to memorize these numeric values right now. Just remember that Java can perform certain automatic conversions between compatible types.
What About the Other Direction?
Now let's say we have a double:
double price = 99.99;
and we want to store it in an int.
This is different.
An int cannot store the decimal part, so Java does not automatically perform this conversion.
For example, this will cause an error:
double price = 99.99;
int amount = price; // Error
Here, we are trying to convert a wider type into a narrower type. Java wants us to explicitly tell it that we understand the conversion.
This is where type casting comes in.
We will learn type casting in the next topic.
Type Conversion vs Type Casting
These two terms are closely related, so beginners often confuse them.
Type conversion is the general process of changing a value from one data type to another. When Java can safely perform the conversion automatically, we call it implicit conversion or widening conversion.
Type casting, on the other hand, is when we explicitly tell Java to convert a value to another type.
For example, automatic conversion:
int number = 10;
double value = number;
Explicit casting:
double number = 10.5;
int value = (int) number;
We will look at explicit casting properly in the next topic.
A Simple Example
Let's see automatic type conversion in a complete program:
class Main {
public static void main(String[] args) {
int marks = 95;
double result = marks;
System.out.println(marks);
System.out.println(result);
}
}
Output:
95
95.0
Java automatically converts the int value into a double.
The important thing to remember is that type conversion doesn't necessarily mean the value itself changes. In this example, 95 simply becomes 95.0 when represented as a double.
One Important Point
Not every data type can be automatically converted into every other data type.
Java allows automatic conversion when the conversion is considered safe, such as:
int → long
int → double
float → double
But converting from a type that can hold more information to one that can hold less information may cause data loss. That's why Java generally requires explicit casting in those situations.
We'll explore that in the next topic.