SoFunction
Updated on 2025-04-14

The difference between const and val in kotlin and analysis of usage scenarios

The difference between const and val in kotlin

In Kotlin,constandvalThey are all used to declare constants, but their usage scenarios and functions are different:

1. val:

  • valUsed to declare read-only variables, that is, variables that are not modified (similar to Javafinalvariable).
  • It can be of any type, including values ​​calculated at runtime.
  • You can givevalA variable is assigned once, and its value cannot be modified after assignment.
val pi = 3.14  // Runtime calculationval name = "Kotlin"  // Runtime calculation

2. const:

  • constUsed to declare compile-time constants. It must be declared in the top level or in the object and can only be used for basic data types (e.g.IntDoubleString)。
  • constyesvalofA special form, its value must be determined at compile time, not calculated at runtime.
  • constKeywords are often used for declarations of constants, and their values ​​are fixed throughout the life of the application.
const val LATEST_VERSION: Int = 4  // Compile time constantsconst val PI: Double = 3.14159  // Compile time constants

Key differences:

  • Assignment timing
    • valThe value of _ is determined at runtime.
    • constThe value of the value must be known at compile time and can only be a basic data type orString
  • Use scenarios
    • valCan be used for any type, including objects, collections, functions, etc.
    • constOnly used for constant values ​​(base data types or strings) and must be declared outside the class or object, usually used for constants.
  • Summarize:
    • If you need to declare constants and want toIts value is determined at compile time, can be usedconst
    • If you just need itDeclare immutable variables, can be usedval

Two Code Example

1 java code

package 
const val LATEST_VERSION: Int = 4  // Compile time constantsconst val PI: Double = 3.14159  // Compile time constantsclass Test4 {
    val pi = 3.14  // Runtime calculation    val name = "Kotlin"  // Runtime calculation}

2 Decompile kotlin code

// 
package ;
import ;
import ;
@Metadata(
   mv = {2, 0, 0},
   k = 1,
   xi = 48,
   d1 = {"\u0000\u001c\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0002\b\u0003\n\u0002\u0010\u0006\n\u0002\b\u0003\u0018\u00002\u00020\u0001B\u0005¢\u0006\u0002\u0010\u0002R\u0014\u0010\u0003\u001a\u00020\u0004X\u0086D¢\u0006\b\n\u0000\u001a\u0004\b\u0005\u0010\u0006R\u0014\u0010\u0007\u001a\u00020\bX\u0086D¢\u0006\b\n\u0000\u001a\u0004\b\t\u0010\n¨\u0006\u000b"},
   d2 = {"Ltest/f/Test4;", "", "()V", "name", "", "getName", "()Ljava/lang/String;", "pi", "", "getPi", "()D", "untitled"}
)
public final class Test4 {
   private final double pi = 3.14;
   @NotNull
   private final String name = "Kotlin";
   public final double getPi() {
      return ;
   }
   @NotNull
   public final String getName() {
      return ;
   }
}
// 
package ;
import ;
@Metadata(
   mv = {2, 0, 0},
   k = 2,
   xi = 48,
   d1 = {"\u0000\u000e\n\u0000\n\u0002\u0010\b\n\u0000\n\u0002\u0010\u0006\n\u0000\"\u000e\u0010\u0000\u001a\u00020\u0001X\u0086T¢\u0006\u0002\n\u0000\"\u000e\u0010\u0002\u001a\u00020\u0003X\u0086T¢\u0006\u0002\n\u0000¨\u0006\u0004"},
   d2 = {"LATEST_VERSION", "", "PI", "", "untitled"}
)
public final class Test4Kt {
   public static final int LATEST_VERSION = 4;
   public static final double PI = 3.14159;
}

3 Summary

val: equivalent to final in java. The value of the variable is determined at runtime, and the value is fixed
const val is equivalent to static final in java, and its value is fixed throughout the life of the application.

This is the end of this article about the difference between const and val in kotlin. For more information about the differences between const and val, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!