Concept Flow - Type casting
Start with a value
Check target type
Perform cast
Use casted value
End
Type casting changes a value from one type to another, either automatically or by explicit instruction.
int i = 100; byte b = (byte) i; System.out.println(b);
| Step | Action | Value Before | Cast Type | Value After | Output |
|---|---|---|---|---|---|
| 1 | Declare int i | N/A | int | 100 | N/A |
| 2 | Cast int i to byte b | 100 | byte | 100 | N/A |
| 3 | Print byte b | 100 | byte | 100 | 100 |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| i | undefined | 100 | 100 | 100 |
| b | undefined | undefined | 100 | 100 |
Type casting changes a value's type. Implicit cast happens automatically if safe. Explicit cast uses (type) before value. Casting from larger to smaller type may lose data. Example: byte b = (byte) intValue;