Literals/Integer: Difference between revisions

Content deleted Content added
→‎{{header|Plain English}}: Added imformation
Ijo Nanpa (talk | contribs)
→‎{{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 (signed 4 byte), namely : decimal, hexadecimal and binary.
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.
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.
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.
<syntaxhighlight lang="scala">// version 1.0.6


Signed and unsigned integer literals can be forced to be interpreted as Long or ULong respectively by appending the suffix 'L' to the literal (lower case 'l' is not allowed as it is easily confused with the digit '1').
fun main(args: Array<String>) {
val d = 255 // decimal integer literal
val h = 0xff // hexadecimal integer literal
val b = 0b11111111 // binary integer literal


<syntaxhighlight lang="kotlin">
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)
val d = 255 // decimal
val h = 0xff // hexadecimal (can use 0X instead of 0x)
val b = 0b11111111 // binary (can use 0B instead of 0b)


// signed long integer literals (cannot use l instead of L)
val sd : Short = 127 // decimal integer literal automatically converted to Short
val sh : Short = 0x7f // hexadecimal integer literal automatically converted to Short
val ld = 255L // decimal
val bd : Byte = 0b01111111 // binary integer literal automatically converted to Byte
val lh = 0xffL // hexadecimal
val lb = 0b11111111L // binary


// 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

// unsigned long integer literals (can use U instead of u)
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 ush : UShort = 0x7fu // hexadecimal unsigned integer literal automatically converted to UShort
val bd : Byte = 0b01111111 // binary signed integer literal automatically converted to Byte

println("$d $h $b $ud $uh $ub $ld $lh $lb $uld $ulh $ulb $ld2 $ush $bd")
}</syntaxhighlight>
}</syntaxhighlight>


{{out}}
{{out}}
<pre>
<pre>
255 255 255 255 255 255 127 127 127
255 255 255 255 255 255 255 255 255 255 255 255 2147483648 127 127
</pre>
</pre>