Type casting is the process of converting a value from one data type to another. Java supports automatic conversions for certain compatible types and explicit conversions using a cast operator.
Type casting converts a value from one data type to another data type.
double price = 99.75;
int amount = (int) price;
System.out.println(amount);
Output:
99
The cast (int) converts the
double value to an integer. The
fractional part is discarded, not rounded.
There are two common categories of primitive numeric conversion:
| Type | Meaning | Example |
|---|---|---|
| Widening | Converts a type to a wider compatible type automatically. | int to double |
| Narrowing | Converts a type to a narrower type using an explicit cast. | double to int |
Widening generally preserves the numeric value, though converting a very large integer to a floating-point type may lose precision. Narrowing can lose information.
Widening conversion changes a value to a compatible type that can represent a broader range of values. It usually happens automatically.
int number = 100;
double result = number;
System.out.println(number);
System.out.println(result);
Output:
100
100.0
Java automatically converts the integer value
into a double value.
The following diagram shows common widening primitive conversion paths:
byte → short → int → long → float → double
char ─────────→ int → long → float → double
A char can widen to an integer type because
it represents a UTF-16 code unit.
char letter = 'A';
int code = letter;
System.out.println(code);
Output:
65
char
can widen to int, but a short
cannot be assigned directly to a char
without an explicit cast.
Narrowing conversion changes a value to a type that has a smaller range or less precision. An explicit cast is generally required for primitive numeric narrowing conversions.
double price = 125.85;
int wholePrice = (int) price;
System.out.println(wholePrice);
Output:
125
The decimal portion is removed when converting this positive floating-point value to an integer.
You can convert an int to a
double automatically or cast a
double to an int explicitly.
int a = 50;
double b = a;
double x = 45.90;
int y = (int) x;
System.out.println(b);
System.out.println(y);
Output:
50.0
45
Remember that converting a floating-point value to an integer truncates the fractional part toward zero.
An integer can be explicitly cast to a
char, and a char can
widen to an integer.
int number = 65;
char letter = (char) number;
System.out.println(letter);
char ch = 'B';
int code = ch;
System.out.println(code);
Output:
A
66
The number 65 corresponds to the character A, and the character B has the numeric code-unit value 66.
A byte stores values from -128 to 127.
Casting an integer outside this range can change
the resulting value.
int number = 130;
byte result = (byte) number;
System.out.println(result);
Output:
-126
The result follows the narrowing conversion rules for the 8-bit signed byte type. The original value cannot be represented as a byte.
Casting can change how arithmetic expressions are evaluated.
int a = 5;
int b = 2;
System.out.println(a / b);
System.out.println((double) a / b);
Output:
2
2.5
In the first expression, integer division discards
the fractional part. In the second, casting
a to double causes
floating-point division.
A string containing digits cannot be converted to an integer using a primitive cast.
Incorrect approach:
String text = "100";
// int number = (int) text; // Compilation error
Correct approach:
String text = "100";
int number = Integer.parseInt(text);
System.out.println(number + 20);
Output:
120
Use methods such as Integer.parseInt()
to parse numeric text. Invalid input can cause a
NumberFormatException.
| Problem | Explanation or Solution |
|---|---|
| Assigning double to int | Use an explicit cast when truncation is intended. |
| Unexpected decimal loss | Converting a floating-point value to an integer discards its fractional part. |
| Integer overflow after casting | Check the target type's range before narrowing. |
| Casting String to int | Use Integer.parseInt() for valid integer text. |
| Unexpected integer division | Convert an operand to a floating-point type before division. |
Suppose a student's marks are stored as a decimal value. You can cast them to an integer when you intentionally want to discard the fractional part.
public class StudentMarks {
public static void main(String[] args) {
double marks = 85.75;
int wholeMarks = (int) marks;
System.out.println("Original Marks: " + marks);
System.out.println("Whole Marks: " + wholeMarks);
}
}
Output:
Original Marks: 85.75
Whole Marks: 85
If you need rounding rather than truncation, use an appropriate rounding method instead of a simple cast.
Write a program that converts a double to an integer and an integer to a double.
public class TypeCastingDemo {
public static void main(String[] args) {
int number = 25;
double decimalNumber = number;
double price = 99.99;
int wholePrice = (int) price;
System.out.println("Integer: " + number);
System.out.println("Double: " + decimalNumber);
System.out.println("Original Price: " + price);
System.out.println("Casted Price: " + wholePrice);
}
}
Output:
Integer: 25
Double: 25.0
Original Price: 99.99
Casted Price: 99
Key Points:
1. Which conversion usually happens automatically?
2. What is the output of this code?
double price = 45.90;
int amount = (int) price;
System.out.println(amount);
3. Which method converts valid integer text to an int?