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
1byte → short → int → long → float → double
java
QUBITS OF DPK
1int age = 25;
2double newAge = age;    // automatic: 25 → 25.0 ✅

2️⃣ Narrowing (Explicit / Casting)

Big type → Small type. Must cast manually. Data loss possible.
java
QUBITS OF DPK
1double price = 99.99;
2int rounded = (int) price;    // 99 — .99 TRUNCATED (not rounded!)

Type Promotion in Expressions

java
QUBITS OF DPK
1byte a = 10;
2byte b = 20;
3byte c = a + b;     // ❌ compile error!
4int  c = a + b;     // ✅ Java promotes byte+byte → int
Java auto-promotes byte, short, char → int during arithmetic.

Promotion Rules

javascript
QUBITS OF DPK
11. byte, short, char → int in expressions
22. One operand is long  → all becomes long
33. One operand is float → all becomes float
44. One operand is double → all becomes double

️ Common Mistakes & Traps

java
QUBITS OF DPK
1// Trap 1 — Casting truncates, doesn't round
2double d = 9.9;
3int i = (int) d;           // 9, NOT 10!
4
5// Trap 2 — Overflow on narrow cast
6int large = 130;
7byte small = (byte) large;  // -126 (bits wrap around!)
8
9// Trap 3 — Integer division before assignment
10double result = 10 / 3;     // 3.0, NOT 3.33! (int/int first)
11double result = 10.0 / 3;   // 3.33 ✅
12
13// Trap 4 — float needs f suffix
14float f = 3.14;             // ❌ 3.14 is double
15float f = 3.14f;            // ✅

Production Use Case

java
QUBITS OF DPK
1// Stripe payment processing — avoid double precision issues
2long amountInCents = (long)(price * 100);
3// Always work in smallest unit, cast carefully

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
1byte a = 10, b = 20;
2byte c = a + b;  // COMPILE ERROR! a+b is promoted to int
3int c = a + b;   // OK
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.