When you are journey starts in java you will obviously come across the concept of type casting in java.
Big Fish in a Small Pond: A Tale of Type Casting in Java
Imagine there’s a big fish swimming in the ocean. The ocean is huge, just like the double data type in Java—it can hold really large and very precise values.
One day, this big fish wants to visit his relatives in a small pond. But here’s the problem: the pond isn’t as big as the ocean. It’s like a int or a byte in Java—it has limited space.
Now, if the big fish (say, a double) wants to squeeze into the small pond (say, an int), he has to shrink himself. This shrinking process is what we call type casting.
Type casting is the process of converting a variable of one datatype to another datatype in simple words, it is a conversion of one data type to another data type.
For Example :
Converting an int into a double
Converting a double into int
Type casting is of two types:
- Widening
- Narrowing
Widening
Widening happens when a smaller data type is converted into a larger Data type.
It is safe no data type is lost.
public class WideningExample {
public static void main(String[] args) {
int num = 20;
double result = num;
System.out.println("Integer: " + num);
System.out.println("Double: " + result);
}
}
OUTPUT
Integer: 20
Double: 20.0
Widening order in Java
byte → short → int → long → float → double
Narrowing
Narrowing Happens when we convert an larger data type into a smaller data type
It needs to be done in a manual manner in parantheses because there is a chance in data loss
public class NarrowingExample {
public static void main(String[] args) {
double num = 45.89;
int result = (int) num; // double → int
System.out.println("Double: " + num);
System.out.println("Integer: " + result);
}
}
OUTPUT
Double: 45.89
Integer: 45
Here the decimal part (.89) is lost.
Type casting With Characters
Characters in java are stored as Unicode values (which are integers). This means you can cast between char and int simple and useful.
public class CharCasting {
public static void main(String[] args) {
char letter = 'A';
int ascii = letter; // char → int
System.out.println("Character: " + letter);
System.out.println("ASCII Value: " + ascii);
}
}
OUTPUT
Character : A
ASCII Value : 65
Here do not get confused how the output of the ASCII Value becomes 65 .
The output is 65 because The Character 'A' has a numeric code of 65.
The above link is the ASCII table. It gives an better understanding on the ASCII values.
Now that you have got an better idea on type casting let us solve an real world example on type casting for better understanding.
Real World Examples of Type Casting
Imagine you are building an E-Commerce application.Prices are stored as double for accuracy,but when displaying rounded values you may want to show them as int.
public class ShoppingCart {
public static void main(String[] args) {
double price = 199.99;
int roundedPrice = (int) price; // double → int
System.out.println("Original Price: " + price);
System.out.println("Rounded Price: " + roundedPrice);
}
}
OUTPUT
Original Price: 199.99
Rounded Price: 199
Here it is converted from double to int using the process of type casting
Here in the above real world example of type casting it is a narrow type casting because we are converting from double to int.
double - 8 byte
int - 4 byte
We have seen what is type casting and its types now we will see why it is important.
Importance of Type Casting
Ensures similarity between different data types
Commonly used in real world applications like unit conversions, money calculation, or working with APIs
API- Application programming interface
Final Thoughts
Type casting in Java is a simple but important concept that ensures smooth interaction between different data types. Whether you’re dealing with mathematical calculations, character values, or application data, mastering type casting will make you a better Java programmer.
Start practicing with different data types (int, double, char, float) and see how casting behaves. With practice, you’ll gain confidence in using type casting effectively in your programs.
Top comments (0)