Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between "const" and "val"?

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?

like image 921
Mathew Hany Avatar asked Jun 02 '16 15:06

Mathew Hany


People also ask

What's the difference between Val and VAR What about const and Val which are mutable and immutable?

var is like a general variable and can be assigned multiple times and is known as the mutable variable in Kotlin. Whereas val is a constant variable and can not be assigned multiple times and can be Initialized only single time and is known as the immutable variable in Kotlin.

Why do we use const in Kotlin?

Use of “const” in Kotlin The const keyword is used to declare properties that are immutable in nature, i.e. read-only properties. The values are only known on the compile-time though, as a result, no values may be assigned at runtime to const variables.

Can Val be changed?

Once a value is assigned to a variable with the val keyword, it cannot be altered or reassigned throughout the program. Val is similar to the final keyword in Java.

What is Val Kotlin?

AndroidMobile DevelopmentApps/ApplicationsKotlin. In Kotlin, we can declare a variable using two different keywords: one is var and the other one is val.


4 Answers

consts are compile time constants. Meaning that their value has to be assigned during compile time, unlike vals, where it can be done at runtime.

This means, that consts 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
like image 162
Luka Jacobowitz Avatar answered Oct 18 '22 22:10

Luka Jacobowitz


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

like image 52
EPadronU Avatar answered Oct 18 '22 20:10

EPadronU


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;
   }
}
like image 35
Jin Wang Avatar answered Oct 18 '22 22:10

Jin Wang


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

  1. The value of the const variable is known at compile time.
  2. 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 -

  1. val is read-only means immutable that is known at run-time
  2. var is mutable that is known at run-time
  3. const are immutable and variables that are known at compile-time
like image 28
Shivam Tripathi Avatar answered Oct 18 '22 21:10

Shivam Tripathi