Skip to content

Commit 9f23b5a

Browse files
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

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

Section5OperatorExpression/src/WideNarrow.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,4 @@ Byte wraps around (because of overflow):
120120
Narrowing (short → byte) works only when the value fits the range.
121121
122122
If the value exceeds the target type's range, overflow occurs.
123-
*/
123+
*/

0 commit comments

Comments
 (0)