Commit 9f23b5a
committed
feat: Demonstrate widening and narrowing type conversions in Java
Added a Java class `WideNarrow` to illustrate primitive type casting:
- **Widening conversions** (implicit) from `short` to `int`, `long`, `float`, and `double`
- **Narrowing conversions** (explicit) from `short` to `byte` and `float` to `int`
Highlights:
- Started with `short s = 100`
- Casted `s` to `byte` (explicit narrowing); value preserved since 100 is within byte range
- Implicitly widened `s` to `int`, `long`, `float`, and `double`; all retain original value
- Demonstrated float to int casting with data truncation (e.g., 10.5 → 10)
Included comments and analysis on:
- Behavior when narrowing exceeds type range (e.g., `short s = 130; byte b = (byte)s;` results in overflow and output -126)
- Safe vs. unsafe conversions based on data range
This program serves as a foundational reference for understanding Java's type conversion mechanisms.
Signed-off-by: Somesh diwan <[email protected]>1 parent c388241 commit 9f23b5a
1 file changed
+1
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
120 | 120 | | |
121 | 121 | | |
122 | 122 | | |
123 | | - | |
| 123 | + | |
0 commit comments