const val
is equivalent to const
in C#. In Kotlin, a const val
:
val
is initialised at runtimeSCREAMING_CASE
(like Java constants)C#
const int port = 80;
readonly string schema = "https";
readonly string host; // Class field
// Later, in the constructor
host = "kotlinlang.org";
host = "microsoft.com"; // ERROR: can only assign once
Kotlin
const val PORT = 80 // As above, type can be inferred
val schema = "https"
val host: String // Can be a class field or local (or even global)
// Later
host = "kotlinlang.org"
host = "microsoft.com" // ERROR: can only assign once