⏱️ 8 min read • Beginner Level • Lesson 17
Completed on . You can revise this lesson or continue to the next topic.
Type casting in Java means converting a value from one data type to another data type. It is commonly used when assigning values between different numeric types or when working with expressions.
Before learning type casting, you should understand Java variables, Java data types, and Java input and output.
Type casting is the process of converting one data type into another.
For example, converting an int value into a double,
or converting a double value into an int.
Type casting helps convert values between compatible data types.
Java supports automatic casting and manual casting.
Type casting is mostly used with numeric data types such as
byte, short, int,
long, float, and double.
Java type casting is mainly divided into two types:
Converting a smaller data type into a larger data type automatically. It is also called implicit casting.
Converting a larger data type into a smaller data type manually. It is also called explicit casting.
Java Type Conversion Hierarchy
Widening casting happens when a smaller data type is converted into a larger data type. Java performs widening casting automatically because there is no risk of data loss.
byte → short → int → long → float → double
public class WideningCastingExample {
public static void main(String[] args) {
int number = 100;
double result = number; // automatic casting: int to double
System.out.println("Integer value: " + number);
System.out.println("Double value: " + result);
}
}
Output:
Integer value: 100 Double value: 100.0
Narrowing casting happens when a larger data type is converted into a smaller data type. Java does not perform narrowing automatically because it may cause data loss. Therefore, narrowing casting must be done manually using parentheses.
smallerType variable = (smallerType) largerValue;
public class NarrowingCastingExample {
public static void main(String[] args) {
float price = 99.75f;
int amount = (int) price; // manual casting: float to int
System.out.println("Flaot price: " + amount);
System.out.println("Integer amount: " + amount);
double value = 99.99;
int number = (int) value; // manual casting: double to int
System.out.println("Double value: " + value);
System.out.println("Integer value: " + number);
}
}
Output:
Float price: 99.75 Integer amount: 99.75 Double value: 99.99 Integer value: 99
double is converted to an
int, the decimal part is removed. It is not rounded.
Type casting is commonly used when working with calculations, user input, APIs, databases, and file processing.
Java follows a natural order for widening numeric conversions. Smaller types can be automatically converted to larger types.
| Conversion Type | Direction | Automatic? |
|---|---|---|
| Widening | byte → short → int → long → float → double |
Yes |
| Narrowing | double → float → long → int → short → byte |
No, manual cast required |
When different numeric types are used in an expression, Java may automatically promote smaller types to larger types before calculation.
public class ExpressionCastingExample {
public static void main(String[] args) {
int a = 10;
double b = 5.5;
double result = a + b; // int is promoted to double
System.out.println(result);
}
}
Output:
15.5
In Java, char values are internally stored as Unicode numbers.
Because of this, a char can be converted to an int
to get its Unicode value.
public class CharCastingExample {
public static void main(String[] args) {
char ch = 'A';
int unicodeValue = ch;
System.out.println("Character: " + ch);
System.out.println("Unicode value: " + unicodeValue);
}
}
Output:
Character: A Unicode value: 65
You can also cast an integer value back to a character.
public class IntToCharCasting {
public static void main(String[] args) {
int number = 66;
char ch = (char) number;
System.out.println(ch);
}
}
Output:
B
In Java expressions, byte and short values are promoted
to int before calculation. This is a common interview question.
public class ByteShortPromotion {
public static void main(String[] args) {
byte a = 10;
byte b = 20;
// byte c = a + b; // Error: result is promoted to int
int result = a + b;
byte c = (byte) (a + b);
System.out.println(result);
System.out.println(c);
}
}
Output:
30 30
byte and
short produce an int result.
Narrowing casting can cause data loss because a larger data type may contain a value that cannot fit into a smaller data type.
public class DataLossExample {
public static void main(String[] args) {
int number = 130;
byte value = (byte) number;
System.out.println(value);
}
}
Output:
-126
byte is from -128 to 127.
Since 130 is outside this range, overflow occurs and the result becomes unexpected.
double directly to an int without casting.
double to int casting to round the number.
byte and short are promoted to int in expressions.
public class CommonCastingMistake {
public static void main(String[] args) {
double price = 99.99;
// int amount = price; // Error: possible lossy conversion
int amount = (int) price; // Correct explicit casting
System.out.println(amount);
}
}
Output:
99
| Type Casting | Also Called | Conversion | Example |
|---|---|---|---|
| Widening Casting | Implicit Casting | Small type to large type | int → double |
| Narrowing Casting | Explicit Casting | Large type to small type | double → int |
| char to int | Unicode Conversion | Character to Unicode number | 'A' → 65 |
| byte/short promotion | Numeric Promotion | byte and short become int in expressions |
byte + byte → int |
char can be converted to int to get its Unicode value.byte and short values are promoted to int in expressions.🧠 Test your understanding with a quick quiz
Topic: Type-casting | Language: Java
int x = 130;
byte y = (byte) x;
System.out.println(y);byte a = 10;
byte b = 20;
byte c = a + b;int num = 100;
double result = num;
System.out.println(result);double price = 99.99;
int amount = (int) price;int a = 10;
double b = a;double value = 45.89;
int number = (int) value;
System.out.println(number);char ch = 'A';
int value = ch;
System.out.println(value);🎉 Great job! Continue learning Java step by step.
Mark this lesson as completed to track your learning progress. To keep track across devices, please login and save your learning progress.
Discussion
Ask questions, share suggestions, or discuss this lesson.