forked from JetBrains/kotlin-web-site
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
doc(docs.topics.numbers): add notes and examples
- Loading branch information
alfredo-toledano
committed
Aug 25, 2024
1 parent
2f82313
commit 8f8f1bc
Showing
2 changed files
with
124 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
fun main() { | ||
// 1. Integer types | ||
// 1.1 infer type | ||
// 1.1.1 Although ANY interger type could be -> infered by default as `Int` | ||
val integerInferredDefaultOne = -126 | ||
println(integerInferredDefaultOne::class.simpleName) | ||
// 1.1.2 if Int's range is exceeded -> type is `Long` | ||
val integerInferredNotDefault = -2147483649 | ||
println(integerInferredNotDefault::class.simpleName) | ||
// 1.2 suffix `L` | ||
val numberWithSuffixLong = 2L | ||
println(numberWithSuffixLong::class.simpleName) | ||
|
||
// 2. Floating-point types | ||
// 2.1 inferred by default as double | ||
val floatingInferredDefaultOne = 3.14 | ||
println(floatingInferredDefaultOne::class.simpleName) | ||
// 2.2 suffix `F` or `f` | ||
val numberWithSuffixFloat = 3.14F | ||
println(numberWithSuffixFloat::class.simpleName) | ||
val numberWithSuffixFloatLower = 3.14f | ||
println(numberWithSuffixFloatLower::class.simpleName) | ||
// 2.3 if you specify > 6-7 decimal digits -> it's rounded | ||
val floatSpecifyingMoreThanValidDecimalDigits = 2.7182818284f | ||
println("floatSpecifyingMoreThanValidDecimalDigits ${floatSpecifyingMoreThanValidDecimalDigits}") | ||
println(floatSpecifyingMoreThanValidDecimalDigits::class.simpleName) | ||
// 2.4 NO implicit widening conversions | ||
fun printDouble(d: Double) { print(d) } // ONLY accept double | ||
val i = 1 | ||
val d = 1.0 | ||
val f = 1.0f | ||
printDouble(d) | ||
//printDouble(i) // Error: Type mismatch | ||
//printDouble(f) // Error: Type mismatch | ||
|
||
// 3. Literal constants for numbers | ||
// 3.1 for integral values | ||
// 3.1.1 decimal | ||
val decimalInt = 123 // Int | ||
val decimalLong = 123L // Long | ||
// 3.1.2 hexadecimal | ||
val hexInt = 0x7B // Int, equivalent to 123 in decimal | ||
val hexLong = 0x7BL // Long, equivalent to 123 in decimal | ||
// 3.1.3 binaries | ||
val binaryInt = 0b1111011 // Int, equivalent to 123 in decimal | ||
val binaryLong = 0b1111011L // Long, equivalent to 123 in decimal | ||
// 3.2 _ | ||
val oneMillion = 1_000_000 | ||
println("oneMillion ${oneMillion}") | ||
val hexBytes = 0xFF_EC_DE_5E | ||
println("hexBytes ${hexBytes}") | ||
val binary = 0b11010010_01101001_10010100_10010010 | ||
println("binary ${binary}") | ||
|
||
// 4. numbers | JVM platform | ||
// 4.1 different representation / boxed | ||
val b: Int = 10000 | ||
val boxedB: Int? = b | ||
val anotherBoxedB: Int? = b | ||
println(boxedB === anotherBoxedB) // false == different boxed objects | ||
// EXCEPT to `Integer` in [-128, 127] | ||
val a: Int = 100 | ||
val boxedA: Int? = a | ||
val anotherBoxedA: Int? = a | ||
println(boxedA === anotherBoxedA) // true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters