Data Types in Kotlin

By | October 8, 2017

Data Types in Kotlin

As you know, a variable is a location in memory (storage area) to hold data. In this section, we are going to learn basic topic data types in kotlin: numbers, characters, booleans, arrays, and strings.

Bitwise operators

left shift, right shift, unsigned right shift, logical and, logical or and exclusive logical or – are supported by Kotlin. Unlike Java, these are not built in operators but named functions instead but can still be invoked like operators:

1
2
3
4
5
6
7
val leftShift = 1 shl 2
val rightShift = 1 shr 2
val unsignedRightShift = 1 ushr 2
val and = 1 and 0x00001111
val or = 1 or 0x00001111
val xor = 1 xor 0x00001111
val inv = 1.inv()

Notice that inverse is not a binary operator, but a unary operator and so is invoked using the dot syntax on a number.

Numbers

Type Bit Rules Value Default
 Double  64  2^63 -1  0.0
 Float  32  2^31 -1  0.0f
 Long  64  2^63 -1  -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807  0L
 Int 32  2^31 -1  -2,147,483,648 to2,147,483,647  0
 Short 16  2^15 -1  -32,768 to 32,767  0
 Byte  8  2^7 -1  -128  to 127  0

 

Booleans

Booleans has two values: true and false.

Character : Characters are represented by the type Char. Character literals go in single quotes: '1'. Special characters can be escaped using a backslash. The following escape sequences are supported: \t\b\n\r\'\"\\ and \$. To encode any other character, use the Unicode escape sequence syntax: '\uFF00'.

 

Strings: strings are represented by the type String

How to declare a variable in Kotlin?

https://www.programiz.com/kotlin-programming/variable-types

next tutorial we learn about type conversion in Kotlin.

Happy Coding

 

 

Leave a Reply