What if your numbers could talk to you clearly instead of hiding in a jumble of digits?
Why Number literal formats (underscore, hex, binary) in Kotlin? - Purpose & Use Cases
Imagine you need to write a long number like a credit card or a big binary value in your code. You type it all as one long string of digits without breaks.
Without clear formatting, it's easy to make mistakes like missing a digit or mixing numbers up. Reading and understanding these long numbers becomes tiring and error-prone.
Kotlin lets you write numbers with underscores to separate groups, and also supports hex and binary formats. This makes numbers easier to read and less likely to have mistakes.
val number = 1000000000 val flags = 15
val number = 1_000_000_000 val flags = 0b1111
You can write big or complex numbers clearly and safely, making your code easier to read and maintain.
When setting color codes in hex or configuring hardware flags in binary, using these formats helps you see exactly what values you are working with.
Underscores improve number readability.
Hex and binary formats let you write numbers in ways that match real-world uses.
This reduces errors and makes code clearer.
Practice
Solution
Step 1: Understand underscore usage in Kotlin numbers
Kotlin allows underscores between digits to improve readability, but they must not be consecutive, leading, or trailing.Step 2: Check each option for correct underscore placement
1_000_000uses single underscores separating groups of three digits correctly. A has consecutive underscores (invalid). B has trailing underscore (invalid). C has consecutive underscores (invalid).Final Answer:
1_000_000 -> Option DQuick Check:
Underscores separate digits properly = 1_000_000 [OK]
- Using double underscores
- Placing underscores at start or end
- Trailing underscores
Solution
Step 1: Review Kotlin rules for underscores in number literals
Underscores can be used between digits but not at the end or start of a number.Step 2: Check each option for trailing underscores
123_456_ends with an underscore, which is invalid syntax. Options A, B, and D are valid hexadecimal, binary, and decimal literals respectively.Final Answer:
123_456_ -> Option AQuick Check:
Trailing underscore invalid = 123_456_ [OK]
- Trailing underscores in numbers
- Confusing hex prefix 0x with decimal
- Misplacing underscores in binary literals
val x = 0xF_A
val y = 0b1010_1010
println(x + y)
Solution
Step 1: Convert hexadecimal and binary literals to decimal
0xF_A means hex FA: F=15, A=10, so 15*16 + 10 = 250. 0b1010_1010 is binary 10101010 which equals 128+32+8+2=170.Step 2: Add the decimal values
250 + 170 = 420.Final Answer:
420 -> Option CQuick Check:
(0xFA + 0b10101010) = 420 [OK]
- Misreading hex digits
- Incorrect binary to decimal conversion
- Ignoring underscores in literals
val num = 0b1021_0010
Solution
Step 1: Understand valid digits in binary literals
Binary literals can only contain digits 0 and 1, optionally separated by underscores.Step 2: Check the given literal for invalid digits
The literal contains '2', which is invalid in binary. Underscores are allowed, and binary literals start with 0b, not 0x.Final Answer:
Binary literals cannot contain digits other than 0 or 1 -> Option BQuick Check:
Binary digits must be 0 or 1 only [OK]
- Using digits other than 0 or 1 in binary
- Confusing hex prefix 0x with binary 0b
- Thinking underscores are disallowed in binary
Solution
Step 1: Convert decimal 255 to binary
255 in binary is 11111111 (eight ones).Step 2: Add underscores for readability
Grouping bits in fours is common, so 1111_1111 is clearer and valid.Step 3: Check options for correctness
0b1111_1111correctly uses underscores and matches 255.0b11111111is correct but less readable.0b1111_1110is 254, and0b1111_11112has invalid digit '2'.Final Answer:
0b1111_1111 -> Option AQuick Check:
Binary 1111_1111 = 255 decimal [OK]
- Using wrong binary digits
- Adding invalid digits like 2
- Not grouping bits for readability
Ready to prove it?
Pass the test and this pattern moves unknown
