Skip to content

Commit eb6b369

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

File tree

1 file changed

+1
-2
lines changed

1 file changed

+1
-2
lines changed

Section10Methods/PracticeProblems/src/Literal.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
public class Literal {
2-
public static void main(String[] args)
3-
{
2+
public static void main(String[] args) {
43
byte b1 = 10;
54
byte b2 = 0b101;
65
byte b3 = 012;

0 commit comments

Comments
 (0)