Literals/Integer: Difference between revisions
Content deleted Content added
Jazzedpineda (talk | contribs) →{{header|Plain English}}: Added imformation |
→{{header|Kotlin}}: Updated the Kotlin example to include unsigned types |
||
Line 1,194:
=={{header|Kotlin}}==
Kotlin supports 3 types of integer literal
A signed integer literal can be assigned to a variable of any signed integer type. If no type is specified, Int (4 bytes) is assumed. If the value cannot fit into an Int, Long (8 bytes) is assumed.
These can be converted to long integer literals (signed 8 byte) by appending the suffix 'L' (lower case 'l' is not allowed as it is easily confused with the digit '1').▼
An unsigned integer literal is made by appending 'u' or 'U' to a signed integer literal. Unsigned literals can be assigned to any unsigned integer type, with UInt (4 bytes) being assumed if none is specified, or ULong (8 bytes) if the value cannot fit into a UInt.
<syntaxhighlight lang="scala">// version 1.0.6▼
▲
val d = 255 // decimal integer literal▼
val h = 0xff // hexadecimal integer literal▼
val b = 0b11111111 // binary integer literal▼
val ld = 255L // decimal long integer literal (can't use l instead of L)▼
fun main() {
val lh = 0xffL // hexadecimal long integer literal (could use 0X rather than 0x)▼
// signed integer literals
val lb = 0b11111111L // binary long integer literal (could use 0B rather than 0b)▼
// signed long integer literals (cannot use l instead of L)
val sd : Short = 127 // decimal integer literal automatically converted to Short▼
val
val
// unsigned integer literals (can use U instead of u)
println("$d $h $b $ld $lh $lb $sd $sh $bd") ▼
val ud = 255u // decimal
val uh = 0xffu // hexadecimal
val ub = 0b11111111u // binary
val uld = 255uL // decimal
val ulh = 0xffuL // hexadecimal
val ulb = 0b11111111uL // binary
// implicit conversions
val ld2 = 2147483648 // decimal signed integer literal automatically converted to Long since it cannot fit into an Int
▲ val
▲ val
}</syntaxhighlight>
{{out}}
<pre>
255 255 255 255 255 255
</pre>
|