Declaring a Variable

Imagine you are writing an adventure game that allows a player to explore an interactive world. You may want a variable for keeping track of the player’s score.

In TypeIntro.kt, create your first variable, called experiencePoints, and assign it a value:

Listing 2.2  Declaring an experiencePoints variable (TypeIntro.kt)

fun main(args: Array<String>) {
    var experiencePoints: Int = 5
    println(experiencePoints)
}

Here, you have assigned an instance of the type Int to a variable called experiencePoints. Let’s walk through each part of what happened.

You defined a variable using the keyword var, which indicates that you want to declare a new variable, followed by the new variable’s name.

Next, you specified the type definition for the variable, : Int, which indicates that experiencePoints will hold an integer (whole number) value.

Last, you used the assignment operator (=) to assign what is on the righthand side (an instance of the Int type, specifically 5) to what is on the lefthand side (experiencePoints).

Figure 2.1 shows the experiencePoints variable’s definition in diagram form.

Figure 2.1  Anatomy of a variable definition

Anatomy of a variable definition

After defining the variable, you print its value to the console using the println function.

Run the program by clicking the run button next to the main function and selecting Run 'TypeIntroKt'. The result printed to the console is 5, the value you assigned to experiencePoints.

Now, try assigning experiencePoints the value "thirty-two" instead. (The strike-through indicates code you are to delete.)

Listing 2.3  Assigning "thirty-two" to experiencePoints (TypeIntro.kt)

fun main(args: Array<String>) {
    var experiencePoints: Int = 5
    var experiencePoints: Int = "thirty-two"
    println(experiencePoints)
}

Run main again by clicking the run button. This time, the Kotlin compiler displays an error:

    Error:(2, 33) Kotlin: Type mismatch: inferred type is String but Int was expected

When you typed this code, you may have noticed the red underline beneath "thirty-two". This is IntelliJ’s signal that the program has an error. Hover over "thirty-two" to read the details of the detected problem (Figure 2.2).

Figure 2.2  Type mismatch disclosure

Type mismatch disclosure

Kotlin uses a static type system – meaning the compiler labels the source code you define with types so that it can ensure the code you wrote is valid. IntelliJ also checks code as you type it and notices when an instance of a particular type is incorrectly assigned to a variable of a different type. This feature is called static type checking, and it tells you about programming mistakes before you even compile the program.

To fix the error, change the value assigned to experiencePoints to an Int that matches its declared type by changing "thirty-two" back to 5:

Listing 2.4  Fixing the type error (TypeIntro.kt)

fun main(args: Array<String>) {
    var experiencePoints: Int = "thirty-two"
    var experiencePoints: Int = 5
    println(experiencePoints)
}

A variable can be reassigned in the course of your program. If the player gains more experience, for example, you can assign a new value to the experiencePoints variable. Add 5 to the experiencePoints variable, as shown:

Listing 2.5  Adding 5 to experiencePoints (TypeIntro.kt)

fun main(args: Array<String>) {
    var experiencePoints: Int = 5
    experiencePoints += 5
    println(experiencePoints)
}

After assigning the experiencePoints variable a value of 5, you use the addition and assignment operator (+=) to add 5 to the original value. Run the program again. You will see the number 10 printed to the console.