Type Conversion in Java
Learn how values are converted from one data type to another in Java using widening conversion, narrowing conversion, and explicit casting.
What is Type Conversion?
Type conversion is the process of converting a value from one data type to another data type.
Java supports both implicit and explicit type conversion. The conversion performed depends on the relationship between the source and target data types.
Types of Type Conversion
Widening Conversion
Converts a smaller numeric type into a larger compatible numeric type automatically.
int number = 100;
long value = number;
Narrowing Conversion
Converts a larger numeric type into a smaller type using an explicit cast.
double price = 99.99;
int value = (int) price;
1. Widening Conversion
Widening conversion occurs when a value is converted from a type with a smaller range to a type with a larger range.
For compatible primitive numeric types, Java can perform this conversion automatically.
int number = 100;
long value = number;
System.out.println(value);
Common Numeric Widening Path
byte
↓
short
↓
int
↓
long
↓
float
↓
double
Widening Example
public class DataTypes {
public static void main(String[] args) {
byte a = 10;
short b = a;
int c = b;
long d = c;
float e = d;
double f = e;
System.out.println(f);
}
}
2. Narrowing Conversion
Narrowing conversion converts a value from a larger numeric type to a smaller numeric type.
Because information can be lost, Java requires an explicit cast.
double number = 100.75;
int value = (int) number;
System.out.println(value);
100
The fractional part is discarded when converting this value to an integer.
Explicit Casting
Explicit casting is performed by placing the target type in parentheses before the value.
double salary = 45000.75;
int roundedValue = (int) salary;
The general syntax is:
targetType variableName = (targetType) value;
Data Loss During Conversion
Narrowing conversion can cause data loss because the target type may not be able to represent the complete source value.
double value = 123.987;
int result = (int) value;
System.out.println(result);
Output:
123
Integer Overflow During Narrowing
If a value is outside the range of the target integer type, narrowing can produce a value that differs significantly from the original value.
int number = 130;
byte value = (byte) number;
System.out.println(value);
The value 130 is outside the range of a Java
byte, which is -128 to 127. The narrowing
conversion therefore keeps the low-order bits and the
resulting value wraps according to two's-complement
representation.
char and Numeric Conversion
A char can participate in numeric conversions
because it is an unsigned 16-bit integral type.
char letter = 'A';
int code = letter;
System.out.println(code);
The output is:
65
Integer Division
Type conversion is particularly important when performing arithmetic operations involving integers.
int a = 5;
int b = 2;
double result = a / b;
The result is 2.0, not 2.5, because
the division is performed using integer operands before the
result is assigned to the double.
To obtain a floating-point result, convert one operand:
int a = 5;
int b = 2;
double result = (double) a / b;
System.out.println(result);
Converting Values to String
Java provides several ways to create a string representation of a value.
int number = 100;
String text = String.valueOf(number);
System.out.println(text);
Converting String to Number
Wrapper classes provide parsing methods for converting text into primitive numeric values.
String text = "123";
int number = Integer.parseInt(text);
System.out.println(number + 10);
Output:
133
String to Double
String text = "45.75";
double value = Double.parseDouble(text);
System.out.println(value);
String to Boolean
String text = "true";
boolean value = Boolean.parseBoolean(text);
System.out.println(value);
Primitive and Wrapper Types
Java provides wrapper classes for primitive types.
| Primitive | Wrapper |
|---|---|
byte |
Byte |
short |
Short |
int |
Integer |
long |
Long |
float |
Float |
double |
Double |
char |
Character |
boolean |
Boolean |
Autoboxing
Autoboxing automatically converts a primitive value into its corresponding wrapper object when required.
int number = 100;
Integer object = number;
Unboxing
Unboxing converts a wrapper object back into its corresponding primitive type.
Integer object = 100;
int number = object;
Type Conversion Quick Reference
| Conversion | Automatic? | Example |
|---|---|---|
| Widening | Yes |
int → long
|
| Narrowing | No |
double → int
|
| String → int | Explicit parsing |
Integer.parseInt()
|
| int → String | Explicit conversion |
String.valueOf()
|
Common Mistakes
- Assuming narrowing conversion is automatic.
- Forgetting that decimal information may be lost when casting to an integer.
- Ignoring overflow when converting to a smaller integer type.
- Expecting integer division to produce a decimal result.
- Forgetting that parsing invalid text can throw a runtime exception.
Summary
Java supports automatic widening conversion and explicit
narrowing conversion. Casting is required when converting
many larger numeric types into smaller ones. Java also
provides parsing methods such as
Integer.parseInt() and
Double.parseDouble() for converting text into
numeric values.