Cosmic Module
J
Qubits of DPK
March 14, 2026
Core Java
Layman Explanation
Imagine a small cup and a big bucket. Pouring from small cup → big bucket is easy (no spill). Pouring from big bucket → small cup requires care (might overflow). That's widening vs narrowing.
Real World Analogy
Converting dollars to cents = widening (more space, no loss). Converting cents to dollars = narrowing (rounding, possible loss).
Two Types
1️⃣ Widening (Implicit / Automatic)
Small type → Big type. Java does it automatically. No data loss.
javascript
QUBITS OF DPK
java
QUBITS OF DPK
2️⃣ Narrowing (Explicit / Casting)
Big type → Small type. Must cast manually. Data loss possible.
java
QUBITS OF DPK
Type Promotion in Expressions
java
QUBITS OF DPK
Java auto-promotes byte, short, char → int during arithmetic.
Promotion Rules
javascript
QUBITS OF DPK
️ Common Mistakes & Traps
java
QUBITS OF DPK
Production Use Case
java
QUBITS OF DPK
Interview Answer (SDE-2)
"Java has two types of conversion. Widening is automatic — smaller type to larger type along the hierarchy byte→short→int→long→float→double. Narrowing is manual using cast operator — data loss through truncation or overflow is possible. Type promotion automatically elevates byte, short, and char to int during arithmetic expressions. Key trap: casting truncates decimals — it doesn't round. Another trap: integer division happens before assignment, so double d = 10/3 gives 3.0 not 3.33."
Interview Questions & MAANG-Level Answers
Q1. What is the difference between widening and narrowing conversion?
Widening (implicit) converts a smaller type to a larger type automatically — no data loss, no cast needed. Path: byte → short → int → long → float → double. Example: int x = 5; double d = x; gives 5.0. Narrowing (explicit) converts a larger type to smaller manually using cast operator — data loss possible via truncation or overflow. Example: double d = 9.7; int x = (int) d; gives 9 (decimal truncated).
Q2. What is type promotion and when does it happen?
During arithmetic expressions, Java automatically promotes smaller types to prevent overflow. Rule: byte, short, char → promoted to int. If any operand is long, result is long. If any operand is float, result is float. If any operand is double, result is double. Example:
java
QUBITS OF DPK
Q3. What is the output of (int) 9.9? Why?
Output is 9. Casting to int truncates the decimal part — it does NOT round. So 9.9 becomes 9, not 10. Similarly, (int)3.99 = 3, (int)(-3.7) = -3. If you want rounding, use Math.round() instead.
Q4. Why does byte c = a + b fail when a and b are bytes?
Because of type promotion. When you write a + b where both are bytes, Java promotes both to int before performing the operation. So the result is an int (4 bytes). Storing an int into a byte (1 byte) without explicit cast is a compile error. Fix: byte c = (byte)(a + b). Note: this can cause overflow if result exceeds -128 to 127.
Q5. What happens when you cast 130 to byte?
You get -126. byte range is -128 to 127, so 130 overflows. Java uses binary two's complement: 130 in binary = 10000010. As a signed byte, the leading 1 means negative, and the value wraps to -126. This is called integer overflow — a classic trap where silently wrong results can cause major bugs in production. Always validate range before narrowing casts.