RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Jun 28, 2021

Creating Variables in Kotlin

Kotlin shares many things in common with the Java programming language. One area where they differ, however, is in the handling of variables. More specifically, the difference lies in data types. Java relies on using wrappers for primitive data types to act as objects, while Kotlin data types are already objects.

Before we get to data types, however, it is important to learn about the different variables Kotlin offers developers. There are two in particular that we will focus on: immutable and mutable. We cover how each type works in this article.

Creating Mutable Variables in Kotlin

Kotlin variables need to be declared prior to being used. If they are not declared, they will toss a syntax error when you try to use them. The type of data you intend to store in your variable gets decided at the time of declaration as well. If the variable is local, the type gets inferred from the value you initialize it with. For example, if we write:

var superIQ = 255

we are creating an Integer type variable. An integer - or Int is essentially a whole number - either negative or positive in value.

We can also create a variable containing text based values, as in the following example:

var superName = "Awkward Man!"

The above example create what is known as a String variable. These types of variables hold letter-based values that include characters, words, and sentences.

The above two variables are known as mutable types of variables. When we use the var keyword to define a variable, it creates a mutable variable. The data contained within mutable variables can be changed. Observe the following code:

var superIQ = 255
println("Your Super Hero IQ is ${superIQ}")
superIQ = 200
println("You bumped your head! Your IQ is now ${superIQ}")

In the above code, we create a mutable variable called superIQ that is of the Int type. We assign it the value of 255, then print the sentence: "Your Super Hero IQ is " and append the current value of superIQ into the sentence. Then, we change the value of superIQ to 200 and print another sentence that also uses the current value of superIQ to showcase that a change has been made to the variable. The result of this code is:

Your Super Hero IQ is 255
You bumped your head! Your IQ is now 200

Creating Immutable Variables in Kotlin

Immutable variables are also known as read-only variables. They differ from mutable variables in that, once data has been assigned to them, that value cannot be changed. To create immutable variables we rely on the val keyword. Here is how you create an immutable variable in Kotlin:

val superIQ = 255

If you try to change the value of an immutable variable, it will result in an error. One more note - you can also create immutable variables by assigning them the value of another variable. Check out the following Kotlin code example:

var superIQ = 255
val myNewSuperIQ = superIQ

The above code creates a mutable variable with the value of 255. We then create an immutable variable that contains the same value as our mutable variable - the value of 255.

Variable Naming Conventions in Kotlin

Naming conventions for variables in Kotlin, regardless of type, follow the lowerCamelCase format. Examples of this include: myIQ, mySuperName, mySuperNameAlias, and so forth. This is part of best coding practices for Kotlin and should be followed.

Variable Scope in Kotlin

Variable scope defines where a variable can be used. In Kotlin, variables can only be used within the block of code in which they were created or declared. By block of code, we mean any code between blocks that begin and end with ({....}). Trying to call the variable or use it outside of the block it was declared will not work and will result in an error.

James Payne
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date