- Numbers: They can be integer types (byte, short, int, long) or of floating point types (float, double). Floating type is denoted by a trailing f at the end.
- Characters: This data type is used to store a single character. A character value must be surrounded by single quotes, e.g. 'a', 'b' etc.
- Boolean: They can have value of either true or false.
- Array: It is a mutable collection of a fixed number of values, called elements, which can be referred by an index. Kotlin arrays are generally created with functions such as, arrayOf() , intArrayOf(), charArrayOf() etc. Use of arrayOf() is quick and convenient way for any data type, string, integer, etc. Constructor of Array class can also be used to create an array, which takes two parameters (size of array, function which accepts index of an array element).
- String: It is a sequence of characters. For example, "Kotlin tutorial at www.zyasin.com!". All strings are objects of String class.
In Kotlin all data types are objects and can be catgorized as one of the following types:
fun main() { // Declaring and assiging values to various number data types. val intNum: Int = 3 // Integer val shortNum: Short = 4000 // short val longNum: Long = 13000000000L // Long val byteNum: Byte = 1 // Byte val doubleNum: Double = 3.99 // Double val floatNum: Float = 25.34f // Float // Print all declared numbers. println("Integer number type value is "+intNum); println("Short number type value is "+shortNum); println("Long number type value is "+longNum); println("Byte number type value is "+byteNum); println("Double number type value is "+doubleNum); println("Float number type value is "+floatNum); }
fun main() { // Declare and assign letter to character type. val letter: Char = 'K' // Print declared character. println("Character letter is "+letter); }
fun main() { // Declaring boolean condition and assigning value to it. val boolVariable: Boolean = true // Printing bioolean value. println("Boolean condition is "+boolVariable); }
fun main() { // Decalre names to some fruits as array. val numbers = arrayOf(1, 2, 3, 4) // or as explicit decalration val numbers = arrayOf(1, 2, 3, 4) // An array can hold mutlple different data types. var mixedArray = arrayOf(1.5, "www.zyasin.com/kotlin.html", 50, 'K') // Modify the second element of number array val numbers = arrayOf[1, 2, 3, 4]. numbers[2] = 7 // Or using set() method. numbers.set(1,7) // Print the third element of number array [1, 7, 3, 4]. println(numbers[3]) // Or using get() method. println(numbers.get(0)) // Print the numbers array as [1, 7, 3, 4]. println(numbers.contentToString()) // Print length of array, val number= array [1,7,3,4]. println(numbers.size) }
fun main() { var text = "Kotlin tuorial at www.zyasin.com/kotlin.html" println(text[0]) // first element/character println(text[12]) // 12th element/character }