Java Type Casting – Complete Guide with Examples and Rules
Learn everything about Java type casting, including implicit (widening), explicit (narrowing) conversion, type promotion, and best practices with examples.
Type Casting in Java – Complete Detailed Tutorial
In Java, type casting is the process of converting a variable from one data type to another.
It allows compatibility between different data types in expressions, assignments, or method calls.
1. Why Type Casting is Needed
- To assign values of one type to another type
- To perform operations on variables of different types
- To prevent data loss or enable precision conversion
Example:
int a = 100;
double b = a; // int to double
2. Types of Type Casting in Java
Java supports two main types of type casting:
- Implicit Casting (Widening)
- Explicit Casting (Narrowing)
2.1 Implicit Casting (Widening Conversion)
- Automatic conversion by the compiler
- Converts smaller data type → larger data type
- Safe, no data loss
- Works for byte → short → int → long → float → double
Example:
int num = 50;
double d = num; // int → double automatically
System.out.println(d); // 50.0
Explanation:
intcan fit intodoublesafely → no explicit cast required
Another Example:
byte b = 10;
int i = b; // byte → int
float f = i; // int → float
System.out.println(f); // 10.0
2.2 Explicit Casting (Narrowing Conversion)
- Manual conversion by the programmer
- Converts larger data type → smaller data type
- May lead to data loss
- Syntax:
(targetType) value
Example:
double d = 99.99;
int i = (int) d; // double → int explicitly
System.out.println(i); // 99 (decimal part lost)
Another Example:
long l = 1000;
short s = (short) l; // long → short
System.out.println(s); // 1000
3. Type Casting Between Primitive Types
| From → ToImplicitExplicit | ||
| byte → short | ✅ | N/A |
| short → int | ✅ | N/A |
| int → long | ✅ | N/A |
| long → int | ❌ | ✅ (int) |
| float → double | ✅ | N/A |
| double → float | ❌ | ✅ (float) |
| char → int | ✅ | N/A |
| int → char | ❌ | ✅ (char) |
4. Type Casting Between Reference Types
- Can cast objects of classes within the same hierarchy
- Use upcasting and downcasting
4.1 Upcasting (Implicit)
- Child → Parent
- Safe and automatic
class Animal {}
class Dog extends Animal {}
public class Test {
public static void main(String[] args) {
Dog d = new Dog();
Animal a = d; // upcasting
System.out.println("Upcasting done");
}
}
4.2 Downcasting (Explicit)
- Parent → Child
- Must be done explicitly, may throw ClassCastException
Animal a = new Dog();
Dog d = (Dog) a; // downcasting
System.out.println("Downcasting done");
5. Type Promotion in Expressions
- Smaller types are automatically promoted in expressions
- Example:
byte b1 = 10;
byte b2 = 20;
int sum = b1 + b2; // byte → int automatically
System.out.println(sum); // 30
6. Example Program – Type Casting
public class TypeCastingDemo {
public static void main(String[] args) {
// Implicit casting
int num = 100;
double d = num; // int → double
System.out.println("Implicit Casting: " + d);
// Explicit casting
double pi = 3.14159;
int intPi = (int) pi; // double → int
System.out.println("Explicit Casting: " + intPi);
// Expression type promotion
byte b1 = 10, b2 = 20;
int sum = b1 + b2;
System.out.println("Type Promotion in expression: " + sum);
}
}
Output:
Implicit Casting: 100.0
Explicit Casting: 3
Type Promotion in expression: 30
7. Summary
- Type casting converts variables from one type → another
- Implicit/Widening: smaller → larger, automatic, safe
- Explicit/Narrowing: larger → smaller, manual, may lose data
- Reference type casting: upcasting (safe), downcasting (requires explicit)
- Type promotion occurs in expressions to avoid data loss