Kotlin – String Constant
To define a String constant in Kotlin, use constant
and val
keyword for the String.
The following is a simple code snippet to define a String as constant which can be accessed using the name GREETING
.
</>
Copy
const val GREETING = "Hello World!"
Examples
In the following program, we will create a String constant in Kotlin using const and val
keywords, and use it in our main function.
Main.kt
</>
Copy
const val GREETING = "Hello World!"
fun main(args: Array<String>) {
print(GREETING)
}
Output
Hello World!
Conclusion
In this Kotlin Tutorial, we learned how to create a String constant in Kotlin.