Commit eb6b369
committed
feat: Add Literal class demonstrating different number literals in Java
WHAT the code does:
- Defines a `Literal` class with a `main()` method.
- Declares four `byte` variables using different literal formats:
- `b1 = 10` → decimal literal.
- `b2 = 0b101` → binary literal (`5` in decimal).
- `b3 = 012` → octal literal (`10` in decimal).
- `b4 = 0xA` → hexadecimal literal (`10` in decimal).
- Prints all four values to the console.
WHY this matters:
- Demonstrates how Java supports multiple **number literal notations**.
- Helps understand different base representations (decimal, binary, octal, hex).
- Useful in low-level programming, debugging, and bitwise operations.
HOW it works:
1. `b1` prints `10` (decimal).
2. `b2` prints `5` (binary `101`).
3. `b3` prints `10` (octal `12`).
4. `b4` prints `10` (hexadecimal `A`).
Tips & gotchas:
- Binary literals require `0b` or `0B` prefix.
- Octal literals require `0` prefix → may cause confusion if leading zeros are unintentional.
- Hex literals use `0x` or `0X` prefix.
- Values must fit within the `byte` range (-128 to 127), otherwise compilation error.
Use-cases:
- Educational example for **Java numeric literal formats**.
- Useful in bit-level programming (e.g., masks, flags).
- Helpful when working with memory addresses or low-level hardware codes.
- Good demonstration for interviews about **binary/octal/hex literals**.
Short key: class-literal-numeric-formats.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent e7b1bda commit eb6b369
1 file changed
+1
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | | - | |
| 2 | + | |
4 | 3 | | |
5 | 4 | | |
6 | 5 | | |
| |||
0 commit comments