Numbers
Integer types
For integer numbers, there are four types:
Type | Size (bits) | Min Value | Max Value |
|---|---|---|---|
| 8 | -128 | 127 |
| 16 | -32768 | 32767 |
| 32 | ||
| 64 |
When you initialize a variable with no explicit type specification, the compiler automatically infers the type with the smallest range enough to represent the value starting from Int.
If it is not exceeding the range of Int, the type is Int. If it exceeds, the type is Long.
Explicit type specification triggers the compiler to check the value not to exceed the range of the specified type.
Unsigned Integer types
Type | Size (bits) | Min value | Max value |
|---|---|---|---|
| 8 | 0 | 255 |
| 16 | 0 | 65535 |
| 32 | 0 | |
| 64 | 0 |
u and U tag is for unsigned literals.
The exact type is determined based on the expected type.
If no expected type is provided, compiler will use UInt or ULong depending on the size of literal:
uL and UL explicitly tag literal as unsigned long:
Floating Point types
Type | Size (bits) |
|---|---|
| 32 |
| 64 |
For variables initialized with fractional numbers, the compiler infers the Double type:
To explicitly specify the Float type for a value, add the suffix f or F.
If such a value contains more than 6-7 decimal digits, it will be rounded:
Literal constants for numbers
There are the following kinds of literal constants for integral values:
Decimals:
123Longs are tagged by a capital L:
123LHexadecimals:
0x0FBinaries:
0b00001011
Underscores can be used to make number constants more readable:
Numbers representation on the JVM
On the JVM platform, numbers are stored as primitive types: int, double, and so on.
Exceptions are cases when you create a nullable number reference such as Int? or use generics.
In these cases numbers are boxed in Java classes Integer, Double, and so on.
Nullable references to the same number can refer to different objects:
All nullable references to a are actually the same object because of the memory optimization that JVM applies to Integers between -128 and 127.
Memory optimization doesn't apply to the b references, so they are different objects.
Explicit number conversions
Smaller types are NOT implicitly converted to bigger types.
All number types support conversions to other types:
toByte() : BytetoShort() : ShorttoInt() : InttoLong() : LongtoFloat() : FloattoDouble() : Double
In many cases, there is no need for explicit conversions because the type is inferred from the context.
Operations on numbers
Kotlin supports the standard set of arithmetical operations over numbers: +, -, *, /, %.
Division between integers numbers always returns an integer number. Any fractional part is discarded.
This is true for a division between any two integer types:
To return a floating-point type, explicitly convert one of the arguments to a floating-point type:
Bitwise Operations
They can be applied only to Int and Long.
Bitwise operations:
shl(bits)– signed shift leftshr(bits)– signed shift rightushr(bits)– unsigned shift rightand(bits)– bitwise ANDor(bits)– bitwise ORxor(bits)– bitwise XORinv()– bitwise inversion
Floating-point numbers comparison
Equality checks:
a == banda != bComparison operators:
a < b,a > b,a <= b,a >= bRange instantiation and range checks:
a..b,x in a..b,x !in a..b