Data Types

Data can have different types.

When you’re solving a math problem, you just write an expression:

5.9 + 6

You know when you add those numbers, you get another number. Kotlin knows that too. You don’t care that one is a fractional number (5.9), which Kotlin calls a Double, and the other is a whole number (6), which Kotlin calls an Int. You know the result is a fractional number, but you probably don’t think too hard about it. Kotlin categorizes these different ways of representing data into types so it knows if you’re using the right kind of data. In the above expression, Kotlin creates a new value of type Double to hold the result.

A type (also called data type) is a classification of data telling the compiler how you intend to use that data. A type provides a set of values from which an expression may take its values. A type defines the operations that can be performed on the data, the meaning of the data, and how values of that type can be stored.

Using types, Kotlin either adapts to what you need, or if you ask it to do something nonsensical it produces an error message. For example, try adding a String and a number:

// DataTypes/StringPlusNumber.kt fun main(args: Array<String>) { println("Sally" + 5.9) } /* Output: Sally5.9 */

In this case, Kotlin has rules that tell it how to add a number to a String. The types are important because Kotlin uses them to figure out what to do. Here, it appends the two values together and creates a String to hold the result.

Now try multiplying a String and a Double by changing the + in StringPlusNumber.kt to a *:

"Sally" * 5.9

Combining types this way doesn’t make any sense to Kotlin, so it gives you an error.

In [var and val], we stored several types, from numbers to letters. Kotlin figured out the type for us, based on how we used it. This is called type inference.

We can be more verbose and specify the type:

val identifier: Type = initialization

You start with the val or var keyword, followed by the identifier, a colon, the type, an =, and the initialization value. So instead of saying:

val n = 1 var p = 1.2

You can say:

val n: Int = 1 var p: Double = 1.2

We’ve told Kotlin that n is an Int and p is a Double, rather than letting it infer the type.

Here are some of Kotlin’s basic types:

// DataTypes/Types.kt fun main(args: Array<String>) { val whole: Int = 11 // [1] val fractional: Double = 1.4 // [2] // true or false: val trueOrFalse: Boolean = true // [3] val words: String = "A value" // [4] val character: Char = 'z' // [5] val lines: String = """Triple quotes let you have many lines in your string""" // [6] println(whole) println(fractional) println(trueOrFalse) println(words) println(character) println(lines) } /* Output: 11 1.4 true A value z Triple quotes let you have many lines in your string */

Kotlin uses type inference to figure out what you mean when you mix types. When mixing Ints and Doubles during addition, for example, Kotlin decides the type for the resulting value:

// DataTypes/Inference.kt fun main(args: Array<String>) { val n = 1 + 1.2 println(n) } /* Output: 2.2 */

When you add an Int to a Double using type inference, Kotlin determines that the result n is a Double and ensures that it follows all the rules for Doubles.

Kotlin’s type inference is part of its strategy of doing work for the programmer. If you leave out the type declaration, Kotlin will usually pick up the slack. If not, it will give you an error message. We’ll see more of this as we go.

Previous          Next

©2018 Mindview LLC. All Rights Reserved.