The difference between const and val in kotlin
In Kotlin,const
andval
They are all used to declare constants, but their usage scenarios and functions are different:
1. val:
-
val
Used to declare read-only variables, that is, variables that are not modified (similar to Javafinal
variable). - It can be of any type, including values calculated at runtime.
- You can give
val
A variable is assigned once, and its value cannot be modified after assignment.
val pi = 3.14 // Runtime calculationval name = "Kotlin" // Runtime calculation
2. const:
-
const
Used 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.Int
、Double
、String
)。 -
const
yesval
ofA special form, its value must be determined at compile time, not calculated at runtime. -
const
Keywords 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:
-
val
The value of _ is determined at runtime. -
const
The value of the value must be known at compile time and can only be a basic data type orString
。
-
-
Use scenarios:
-
val
Can be used for any type, including objects, collections, functions, etc. -
const
Only 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 used
const
。 - If you just need itDeclare immutable variables, can be used
val
。
- If you need to declare constants and want toIts value is determined at compile time, can be used
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!