Overview:

In this tutorial, we are going to take a look at Kotlin. Specifically, we will explore the default Types in Kotlin and learn how to define variables. If you’re already familiar with Java – or oddly enough, TypeScript – you will find that defining variables in Kotlin is easy to understand and provides some simplified syntax compared to it other programming languages.

Requirements:

  • None
Source Code:
https://github.com/codetober/kotlin-types-variables

Watch the Video Instead:

Defining a Variable

In Kotlin, similar to other languages, there are many ways to define a new variable. We will be taking a look at three (3) types of variables: var, val,  and const.

When you define a variable as var, it is mutable. Meaning, that you are able to re-assign the value of a var as many times as you please. However, defining a val creates an immutable variable that cannot have its value changed during the runtime of the application. In the example below, we have defined variables both inside of our main function and outside.

If you define a val, it cannot be reassigned later in your code. In any IDE that supports Kotlin (I am using IntelliJ IDEA) trying to do so will cause an error: Val cannot be reassigned.

Generally speaking, it’s a good idea to use const when you want to define a variable that will NEVER change and that you want to be protected from change. Below we have defined a variable that is both outside of the main function and is a val. Doing this allows the Kotlin compiler to optimize behind the scenes.

// Compile-time Constants
const val creator = "CodeTober"


fun main(args: Array<String>) {

    // Assign the String value 'developer' to the role variable, then print
    var role: String = "developer"
    println(role)

    /*
     Uncomment the next line to see an inferred type exception
     because the Kotlin compiler is expecting an Int (whole number)
     and we gave it a String instead
    */
    // var age: Int = "26"

    // Read-Only variable
    val firstname: String = "User"
    println(firstname)

    // If you uncomment the next line, you will see 'cannot reassign' error
    //name = "someone else"
}

Kotlin Types

Kotlin supports most of the built-in types you would expect from a modern language. You have already seen a few of these in the example code for the previous section of this guide.

Kotlin Types:
String Multiple characters or words“The dog needs to go out!”
CharSingle character‘C’
BooleanTrue or Falsetrue
IntWhole Numbers10
DoubleDecimal Numbers11.01876
ListA basic Collection“WI”, “MW”, “FL”, “WI”
SetA Collection with unique entries“1”, “2”, “3”, “4”
MapA Collection of Key-Value Pairs“name”: “user, “role”: “developer”, “lastname”: “power”

When you define a variable in most strictly typed languages, you must provide the type of the variable you are defining. In some languages like JavaScript, supplying the type is not required. Kotlin, even though it is a strongly typed language allows you (even prefers) to not provide a type when a variable is defined. It does this by using Type Inference – when Kotlin is compiled it will choose the proper type.

Let’s break down a basic variable definition and examine the parts:
var firstname: String = "user"

In the above definition, var, lets Kotlin know that the variable may change its value in the future. firstname, is the name of the variable. :String, is the type of the variable, notice that it is preceded by a colon; this separates the variable name from the type definition. = "user" is the value that you are initializing the variable with.

Optionally, a variable is defined using Type Inference:
var firstname = "user"

The String part of this definition is added by the compiler at runtime.

View the source code for this tutorial to see the full syntax for variable definition and initializing Collections, such as Maps, Lists, and Sets.

 

That’s it! Congratulations on making it all the way through this tutorial and continuing your quest to craft amazing code! If you liked this tutorial, please share it 🙂 Comment below with any questions or issues you faced.