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: | Line 1,194: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
Kotlin supports 3 types of integer literal |
Kotlin supports 3 types of integer literal: decimal, hexadecimal and binary. |
||
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. |
|||
⚫ | |||
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. |
|||
It is also possible to assign integer literals to variables of type Short (signed 2 byte) or Byte (signed 1 byte). They will be automatically converted by the compiler provided they are within the range of the variable concerned. |
|||
⚫ | |||
⚫ | |||
fun main(args: Array<String>) { |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
fun main() { |
|||
⚫ | |||
// signed integer literals |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
// signed long integer literals (cannot use l instead of L) |
|||
⚫ | |||
val |
val ld = 255L // decimal |
||
val |
val lh = 0xffL // hexadecimal |
||
⚫ | |||
// unsigned integer literals (can use U instead of u) |
|||
⚫ | |||
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 |
|||
⚫ | |||
⚫ | |||
⚫ | |||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
255 255 255 255 255 255 |
255 255 255 255 255 255 255 255 255 255 255 255 2147483648 127 127 |
||
</pre> |
</pre> |
||