I have recently read about the const
keyword, and I'm so confused! I can't find any difference between const
and the val
keyword, I mean we can use both of them to make an immutable variable, is there anything else that I'm missing?

- 14,073
- 11
- 62
- 81

- 13,310
- 4
- 19
- 16
-
3https://kotlinlang.org/docs/reference/properties.html#compile-time-constants – Michael Jun 02 '16 at 15:34
-
1You can read this article https://www.android4dev.com/difference-between-var-val-and-const-in-kotlin/ or Watch this video https://www.youtube.com/watch?v=DQLrEGqSSI8&t=6s – Lokesh Desai Jun 05 '20 at 15:59
9 Answers
const
s are compile time constants. Meaning that their value has to be assigned during compile time, unlike val
s, where it can be done at runtime.
This means, that const
s can never be assigned to a function or any class constructor, but only to a String
or primitive.
For example:
const val foo = complexFunctionCall() //Not okay
val fooVal = complexFunctionCall() //Okay
const val bar = "Hello world" //Also okay

- 2,915
- 1
- 24
- 48

- 22,795
- 5
- 39
- 57
-
4What about something like this: `const val foo = "Hello world"` and `val bar = "Hello world"`? Are they the same? – Mathew Hany Jun 02 '16 at 15:32
-
5@MathewHany, at least not in terms of bytecode, see: http://stackoverflow.com/questions/37482378/static-data-in-kotlin/37485356#37485356 – hotkey Jun 02 '16 at 15:34
-
5I think `const` values will just be completely inlined during compilation. – Luka Jacobowitz Jun 02 '16 at 15:35
-
173This begs another question: Why does Kotlin require `const val` instead of just `const`? It seems to me the `val` keyword is totally superfluous in this context, since `const var` would be absurd on its face. – Eric Lloyd Jun 08 '17 at 15:51
-
3Then why the differentiation? Why can't the compiler determine if a value is a const and compute its value at compile time if it is? – Gonzalo Mar 23 '18 at 14:39
-
`const val` is underlined with red with a message: `Modifier 'const' is not applicable to 'local variable'` inside a function. – CoolMind Mar 26 '18 at 12:17
-
47@EricLloyd With `const val`, `const` is a modifier on `val` rather than a keyword. Modifiers > keywords. More examples of this same design are, `annotation/enum/data class`, `private val`, `inline fun`, etc. – Aro Apr 02 '18 at 19:36
-
1since consts are assigned at compile time, when the object is destroyed by GC will the constants be free or do they reside forever ? – j2emanue Jan 08 '19 at 11:15
-
@Aro So in other words, a constant modifying a constant. Yeah makes sense (rolls eyes). – Johann May 27 '19 at 14:31
-
@EricLloyd A `const` is inlined by the compiler which you don't always want because it requires recompilation in order the change the value – Florian Walther Oct 09 '19 at 08:51
-
2@FlorianWalther Yeah, that's kinda the idea -- I typically use `const`s for truly immutable values -- HTTP verbs, the value of Pi, etc. If it's a mutable or even configurable value, a `const` isn't the answer. – Eric Lloyd Oct 15 '19 at 06:22
-
seeing how Kotlin is superior because it reduces nosiness of unnecessary tings, "val" in "const val" is totally useless, unless of course you can write "const var" which is nonsense. – Dainius Jan 21 '21 at 08:01
-
@EricLloyd totally agree with you. However, comparing with all the superises brough by `const` and `constexpr` from c++11 to c++20, I would say, this is reality, nothing is perfect. – r0n9 May 18 '22 at 22:56
-
1@Dainius, I'm guessing it's done out of necessity because `const` is a soft keyword. You can use it as a variable or function name. If you could write `const` without `val`, it would have to be a hard keyword. – Tenfour04 May 26 '22 at 15:34
Just to add to Luka's answer:
Compile-Time Constants
Properties the value of which is known at compile time can be marked as compile time constants using the const modifier. Such properties need to fulfill the following requirements:
- Top-level or member of an object declaration or a companion object.
- Initialized with a value of type String or a primitive type
- No custom getter
Such properties can be used in annotations.
Source: Official documentation
You can transform the Kotlin to Java. Then you can see const has one more static modifier than val. The simple code like this.
Kotlin:
const val str = "hello"
class SimplePerson(val name: String, var age: Int)
To Java(Portion):
@NotNull
public static final String str = "hello";
public final class SimplePerson {
@NotNull
private final String name;
private int age;
@NotNull
public final String getName() {
return this.name;
}
public final int getAge() {
return this.age;
}
public final void setAge(int var1) {
this.age = var1;
}
public SimplePerson(@NotNull String name, int age) {
Intrinsics.checkParameterIsNotNull(name, "name");
super();
this.name = name;
this.age = age;
}
}

- 671
- 6
- 8
-
3Could someone state in a comment why this answer was downvoted to oblivion? – James Jordan Taylor Nov 04 '18 at 19:39
-
3@JamesJordanTaylor I upvoted. But I assume it's because some people didn't read it carefully, and at a quick glance this answer seems to be talking about how to convert from java to kotlin, which would be off-topic. – WSBT Nov 07 '18 at 23:19
-
2
-
3@DYS: I think it will remove the "static" and it will be just public final String str = "hello"; – Varun Ajay Gupta Mar 03 '20 at 15:25
-
@DYS compare it to `SimplePerson`'s `private final String name;` which doesn't have the const and then is private as well, but that's because it's a member val instead of a top-level/package val and not because of the `const`. – nobled Jul 18 '21 at 06:18
const kotlin to Java
const val Car_1 = "BUGATTI" // final static String Car_1 = "BUGATTI";
val kotlin to Java
val Car_1 = "BUGATTI" // final String Car_1 = "BUGATTI";
In simple Language
- The value of the const variable is known at compile time.
- The value of val is used to define constants at run time.
Example 1-
const val Car_1 = "BUGATTI" ✔
val Car_2 = getCar() ✔
const val Car_3 = getCar() ❌
//Because the function will not get executed at the compile time so it will through error
fun getCar(): String {
return "BUGATTI"
}
This is because getCar() is evaluated at run time and assigns the value to Car.
Additionally -
- val is read-only means immutable that is known at run-time
- var is mutable that is known at run-time
- const are immutable and variables that are known at compile-time

- 640
- 8
- 7
Both val
and const
are immutable.
const
is used to declare compile-time constants, whereas val
for run-time constants.
const val VENDOR_NAME = "Kifayat Pashteen" // Assignment done at compile-time
val PICon = getIP() // Assignment done at run-time

- 579
- 1
- 5
- 14
-
-
2@whatwhatwhat yes. The code is compiled before being sent for execution. The point of time when the code executes is what is essentially known as run-time execution. – Arpan Sircar Mar 01 '21 at 15:43
-
1
-
Because I read a lot, that "val" means immutable: This is definitely not the case, just see this example:
class Test {
var x: Int = 2
val y
get() = x
}
fun main(args: Array<String>) {
val test = Test()
println("test.y = ${test.y}") // prints 2
test.x = 4
println("test.y = ${test.y}") // prints 4
}
Sadly, true immutability you can currently only expect with const - but this only at compile time. At runtime you can't create true immutability.
val just means "readonly", you can't change this variable directly, only indirect like I have shown in the example above.

- 129
- 1
- 8
-
-
1it *is* immutable even in your example. you defined y as a *function* which returns whatever is in x. this function cannot be re-assinged to another function – Marko Bjelac Jan 20 '23 at 09:45
-
You are just returning a value of another variable through overriden getter method! You are NOT MUTATING Y i.e not reassigning it! – Mohammed Junaid Jan 25 '23 at 11:15
val
Kotlin val
keyword is for read-only properties in comparison with Kotlin var
keyword. The other name for read-only
property is immutable
.
Kotlin code:
val variation: Long = 100L
Java equivalent looks like this:
final Long variation = 100L;
const val
We use const
keyword for immutable properties too. const
is used for properties that are known at compile-time. That's the difference. Take into consideration that const
property must be declared globally
.
Kotlin code (in playground):
const val WEBSITE_NAME: String = "Google"
fun main() {
println(WEBSITE_NAME)
}
Java code (in playground):
class Playground {
final static String WEBSITE_NAME = "Google";
public static void main(String[ ] args) {
System.out.println(WEBSITE_NAME);
}
}

- 49,178
- 17
- 136
- 220
-
Read-only is not the same thing as immutable so the second sentence of this answer is false. You can have a read-only `val` that produces different results on multiple calls through a custom getter or because it’s a delegated property, or because it’s open and has a setter in a subclass – Tenfour04 Feb 28 '22 at 19:42
-
"Kotlin val keyword is for read-only properties" if so then why do you write to it in your example? – Marian Paździoch Apr 26 '22 at 13:16
-
@Tenfour04 read-only === immutable, see my comment: https://stackoverflow.com/questions/37595936/what-is-the-difference-between-const-and-val#comment132669180_70486344 – Marko Bjelac Jan 20 '23 at 09:47
-
@MarkoBjelec Counterexamples to what you're saying: https://pl.kotl.in/DZ-c6drq0 – Tenfour04 Jan 20 '23 at 14:03
For those who are looking which is more appropriate or efficient between val
and const
:
For the String or any primitive data type const val
is recommended to use instead of val
. Because val
will be known at runtime, so when your app is running then it will process all the values. On other hand const val
will do this earlier at compile time. So performance wise const val
will give better result.

- 5,949
- 5
- 36
- 46
Let's learn this by an example.
object Constants {
val NAME = "Amit"
}
Note: We are not using const
.
And, we are accessing this NAME
as below:
fun testValWithoutConst() {
val name = Constants.NAME
}
Now, we need to decompile this code. For that, we will have to convert this Kotlin source file to a Java source file.
We will get the following output:
public final void testValWithoutConst() {
String name = Constants.INSTANCE.getNAME();
}
The output is as expected.
The above example was without the const
keyword. Now, let's use the const
keyword.
For that, we will modify our object class Constants
in Kotlin as below:
object Constants {
const val NAME = "Amit"
}
Note: We are using const
.
And, we are accessing this NAME
as below:
fun testValWithConst() {
val name = Constants.NAME
}
Now, when we decompile this code, we will get the following output:
public final void testValWithConst() {
String name = "Amit";
}
Here, we can see that the variable NAME
has been replaced by its value which is Amit
.
As the value has been inlined, there will be no overhead to access that variable at runtime. And hence, it will lead to a better performance of the application.
This is the advantage of using const
in Kotlin.
Reference from my blog: Advantage of using const in Kotlin

- 3,135
- 2
- 16
- 17