Generic Top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE

1. Introduction

In this tutorial, we’ll be outlining the key differences between the const, var, and val keywords in the Kotlin language.

To put these keywords into context, we’ll be comparing them to their Java equivalents.

2. Understanding Typing

To understand these keywords, we have to understand two of the major categories of type systems a language can follow – manifest typing and inferred typing.

2.1. Manifest Typing

All languages offer a range of primitive data types to store and manipulate data within a program. Programming languages following the manifest typing discipline must have their data types explicitly defined within the program.

Java, up until version 10, strictly follows this discipline. For example, if we want to store a number within a program, we must define a data type such as int:

int myVariable = 3;

2.2. Inferred Typing

Unlike Java, Kotlin follows the inferred typing discipline. Languages supporting type inference automatically detect data types within the program at compile-time.

This detection means that we, as developers, don’t need to worry about the data types we’re using.

3. var

Firstly, we’ll start with var, Kotlin’s keyword representing mutable, non-final variables. Once initialized, we’re free to mutate the data held by the variable.

Let’s take a look at how this works:

var myVariable = 1

Behind the scenes, myVariable initializes with the Int data type.

Although Kotlin uses type inference, we also have the option to specify the data type when we initialize the variable:

var myVariable: Int = 1

Variables declared as one data type and then initialized with a value of the wrong type will result in an error:

var myVariable: Int = b //ERROR!

4. val

Kotlin’s val keyword works much in the same as the var keyword, but with one key difference – the variable is read-only. The use of val is much like declaring a new variable in Java with the final keyword.

For example, in Kotlin, we’d write:

val name: String = "Baeldung"

Whereas in Java, we’d write:

final String name = "Baeldung";

Much like final variables in Java, val variables must be assigned at declaration, or in a Class constructor:

class Address(val street: String) {
    val name: String = "Baeldung"
}

5. const

Like val, variables defined with the const keyword are immutable. The difference here is that const is used for variables that are known at compile-time.

Declaring a variable const is much like using the static keyword in Java.

Let’s see how to declare a const variable in Kotlin:

const val WEBSITE_NAME = "Baeldung"

And the analogous code written in Java would be:

final static String WEBSITE_NAME = "Baeldung";

6. Conclusion

In this article, we’ve taken a quick look at the difference between manifest and inferred typing.

Then, we looked at the difference between Kotlin’s var, val, and const keywords.

Generic bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE

newest oldest most voted
Notify of
The Pete
Guest
The Pete

Regarding val and “the data is immutable”:
The reference to the data can’t be changed but the data itself doesn’t have to be immutable.

class User(var name: String)

val user = User(“Baeldung”)
val user = User(“Foo”) // compiler error
user.name = “Foo” // works just fine

Eric Martin
Member
Eric Martin

You are correct. I’ve changed it to ‘the variable is read-only’. This matches Kotlin’s documentation.