What is the difference between “const” and “val”?

前端 未结 7 2078
既然无缘
既然无缘 2020-12-22 17:20

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

相关标签:
7条回答
  • 2020-12-22 17:55

    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
    
    0 讨论(0)
  • 2020-12-22 18:01

    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
    
    0 讨论(0)
  • 2020-12-22 18:07

    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

    0 讨论(0)
  • 2020-12-22 18:15

    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;
       }
    }
    
    0 讨论(0)
  • 2020-12-22 18:15

    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
    0 讨论(0)
  • 2020-12-22 18:17

    In kotlin, const and val both represents the immutability and read only values and act as final keyword in java.

    val keyword must be used to declare for run time values and const keyword must be used to declare compile time values.

    Keep in mind, const must be used only with primitive data types not for function and constructors.

    Example -
    

    const val fun1 = anyFunctionOrConstructor() // it is not fine
        val fun2 = anyFunctionOrConstructor() // it is perfectly fine
        
        const val aa = "My String" // it is perfectly fine

    0 讨论(0)
提交回复
热议问题