1. Introduction
Type casting in Java is the process of converting a variable from one data type to another. Because Java is strongly typed, such conversions must either happen automatically when safe, or be explicitly requested by the programmer when there is a risk of data loss.
Cricket analogy: Converting a promising U-19 player to the senior squad is automatic when clearly ready (safe promotion), but moving a senior star down to domestic cricket requires an explicit selectorial decision, like Java's automatic-vs-explicit casting.
There are two main categories of primitive type casting: widening (implicit) casting, where a smaller type is automatically converted to a larger type, and narrowing (explicit) casting, where a larger type is manually converted to a smaller type using a cast operator.
Cricket analogy: Moving a domestic player up to international cricket is like widening — automatic once form justifies it — while forcing an international star to play in a lower domestic league is like narrowing, requiring an explicit, deliberate decision with risk of misuse.
2. Syntax
2.1 Widening (Implicit) Casting
byte -> short -> char -> int -> long -> float -> double
int num = 100;
double d = num; // implicit widening, no cast needed
System.out.println(d); // 100.02.2 Narrowing (Explicit) Casting
double price = 99.99;
int roundedDown = (int) price; // explicit cast required, truncates decimal
System.out.println(roundedDown); // 993. Explanation
Widening casting is automatic because converting from a smaller type to a larger type never loses information — for example, converting int to double is always safe since double can represent every int value (plus a fractional part). The widening order for numeric types is: byte -> short -> int -> long -> float -> double (char also widens directly to int).
Cricket analogy: Widening is safe like a player moving up the ranks byte->short->int->long->float->double, roughly matching U-14->U-16->U-19->A-team->national->legend, and a single-letter nickname (char) widens naturally into a full player ID (int) without losing meaning.
Narrowing casting is explicit and requires the programmer to write a cast operator, such as (int) or (byte), because it can lose data or precision. For example, casting a double to an int truncates (removes) the fractional part rather than rounding — 99.99 becomes 99, not 100. Casting a large long into an int can also overflow and produce unexpected values if the value doesn't fit.
Cricket analogy: Narrowing is like forcing an international star (long) back into a domestic XI (int) — some value gets lost or overflows into odd stats; and truncating a 99.99 batting-average-like double to 99 by casting to int just chops the decimal, it doesn't round up.
Java also supports autoboxing and unboxing between primitive types and their corresponding wrapper classes (int <-> Integer, double <-> Double, etc.). Autoboxing is the automatic conversion of a primitive into its wrapper object (e.g., Integer i = 5;), and unboxing is the reverse (e.g., int x = i;). This happens automatically since Java 5 but has a performance cost and can throw a NullPointerException during unboxing if the wrapper object is null.
Cricket analogy: Autoboxing is like a scorer automatically writing a bare run count onto an official scorecard (int->Integer object), and unboxing reads it back off; but if the scorecard slot is blank (null Integer), trying to read a number throws an NPE, and constantly boxing every run has its own overhead.
Exam trap: (int) 99.99 truncates to 99, it does NOT round. To round, use Math.round(99.99), which returns 100. Also, unboxing a null Integer (int x = someNullInteger;) throws a NullPointerException at runtime, a very common interview gotcha.
4. Example
public class TypeCastingDemo {
public static void main(String[] args) {
// Widening (implicit)
int intValue = 50;
long longValue = intValue; // int -> long, implicit
double doubleValue = longValue; // long -> double, implicit
System.out.println("Widening: " + longValue + ", " + doubleValue);
// Narrowing (explicit)
double d = 9.78;
int narrowed = (int) d; // explicit cast, truncates
System.out.println("Narrowing: " + narrowed);
// Autoboxing and unboxing
Integer boxed = 10; // autoboxing: int -> Integer
int unboxed = boxed; // unboxing: Integer -> int
System.out.println("Boxed: " + boxed + ", Unboxed: " + unboxed);
}
}5. Output
Widening: 50, 50.0
Narrowing: 9
Boxed: 10, Unboxed: 106. Key Takeaways
- Widening casting (small to large type) is automatic and lossless.
- Narrowing casting (large to small type) requires an explicit cast operator and can lose data.
- Narrowing a decimal to an integer truncates the fractional part, it does not round.
- Autoboxing converts a primitive to its wrapper class automatically; unboxing does the reverse.
- Unboxing a null wrapper object throws a NullPointerException.
- Widening order: byte -> short -> int -> long -> float -> double.
Practice what you learned
1. What is the result of (int) 9.99 in Java?
2. Which of these conversions is done implicitly (widening) by Java?
3. What does autoboxing refer to in Java?
4. What happens when you unbox a null Integer into an int?
5. Which cast is required to convert a long to a byte?
Was this page helpful?
You May Also Like
Data Types in Java
A complete guide to Java's 8 primitive data types with exact sizes and ranges, plus reference types like String and arrays.
Variables in Java
Learn what variables are in Java, how to declare and initialize them, naming rules, and the difference between local, instance, and static variables.
Operators in Java
Explore Java's arithmetic, relational, logical, bitwise, assignment, unary, ternary, and instanceof operators along with operator precedence.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics