Updated March 30, 2023
Introduction to Kotlin const
The kotlin const is one of the immutability keywords, and it accepts read-only values; it acts as the other keywords like final in java. The const keyword should be used to declare the compile-time values. The compile-time constants are meaned that their values that has to be assigned during the compile time. It assigned only the String or other primitive datatypes. It does not and never has been assigned to the any function or any class constructors so that it’s the Top-level or object member declaration, and also its referred as the companion object; it has no custom getter values.
Syntax of Kotlin const
Kotlin language has many default keywords, variables, and other functions for building and constructing the kotlin mobile-based applications. Like that const, val are the keywords for the identifiers used to declare the read-only values the users are unable to change it.
val vars;
const val vars1;
object name{
val vars2;
const val vars4;
}
fun main(args:Array<String>)
{
--some logic codes depends on the requirement—
}
The above codes are the basic syntax for declaring the const keyword values on the code, and it is called whenever needed on the flow.
How does const work in Kotlin?
The const is one of the keywords, and it is used to declare the values on the kotlin language. It means once we declared the values, it does not change; it accepts only the read-value access. In kotlin, if we want to create the local constants and it is supposed to be used with in the class, we use the val keyword to declare the values with reference names. If we want to create the public constant, then we used the public keyword; additionally, if we used other methods like static, final keywords static is another keyword, and it is used to declare and create the one instance, and it is shared across all the cases of the class.
The final keyword is the fixed value which does not change it anywhere of the same and different classes. It’s the Top-level approach, and the value is declared the const on the class file that can be used to mention the clear rations with the same class. We can also declare the separated and dedicated constants.kt file for to store the global const values and reference may be used to call anywhere of the project with the help of the configurations like call the url paths of the class and imported the kotlin packages.
Examples of Kotlin const
Given below are the examples of Kotlin const:
Example #1
Code:
interface first {
fun demo() {
println("Thank you for declaring the first interface with the method using for declare the application code")
}
}
interface second {
fun demo1() {
println("Its the second interface for declaring the method and this features for to write the kotlin codes")
}
}
const val vars = "Have a Nice day users please try again"
val vars2 = demo2()
fun demo2(): Double {
return 4.567
}
class Sample: first, second
fun main(args: Array<String>) {
println("Welcome To My Domain its the first example that related to the kotlin constant keyword")
val ob = Sample()
ob.demo()
ob.demo1()
println("The kotlin const keyword which uses to declare the $vars")
println("Value of the double type: $vars2")
var a = 200
println("The value of a is = $a");
var b = 3000L
println("The value of b is = $b")
var c = 0x0C
println("The value of c is = $c")
var d = 0b10
println("The value of d is = $d")
var e = 342_323_313_300
println("The value of e is= $e");
var f = 4000_40000L
println("The value of f is = $f")
var g = 0x0C_0c_0f
println("The value of g is = $g")
var h = 0b10_1000_10
println("The value of h is = $h")
}
Output:
In the above example, we used const values along with other format-type values.
Example #2
Code:
const val vars = "The value is fixed one and it cannot be changed and it is used whenever and wherever its necessary calls on the flow"
val vars2 = test()
fun test(): String {
return "Thank you users for creating the method in the kotlin const concepts"
}
data class Months(var mnthname: String, var day: String)
fun main() {
println("Welcome To My Domain is the second example that related to the kotlin const concepts")
val mnths: List<Months> = listOf(
Months("January", "jan"),
Months("February", "feb"),
Months("March", "mar"),
Months("April", "apr"),
Months("May", "may"),
Months("June", "jun"),
Months("July", "jul"),
Months("August", "aug"),
Months("September", "sept"),
Months("October", "oct"),
Months("November", "nov"),
Months("December", "dec")
)
val a: Map<String, String> = mnths.associate { Pair(it.mnthname, it.day) }
println(a)
val b: Map<String, String> = mnths.associateBy({it.mnthname}, {it.day})
println(b)
val map: Map<String, String> = mnths.map { it.mnthname to it.day }.toMap()
println(map)
val d: MutableMap<String, String> = HashMap()
for (Months in mnths) {
d[Months.mnthname] = Months.day
}
println(d)
var vars3 = setOf(23,46,69,92,"String","Raman")
for(x in vars3){
println("$x,vars3.flatMap{it.toList()}")
}
val vars4 = setOf(2, 4, 6, 8, 10)
println("If your input numbers are not null values: "+vars4.mapNotNull
{ if ( it == 1) null else it * 2 })
println("If your input numbers are not null values: "+vars4.mapIndexedNotNull
{ vars5, value -> if (vars5 == 0) null else value * vars5 })
val vars6: Set<Any> = setOf(0,1,2,3,4,5,"siva","raman")
val vars7 = setOf(4,8,12,16,20)
println("Have a nice day users")
for(y in vars6){
println("$y,vars7.flatMap{it.toList()}")
}
println("Your input datas are")
println(vars6.contains("siva"))
println("Your second input datas should be the number format")
println(vars6.contains(41))
println("Your third input datas should be the numeric characters")
println(vars6.containsAll(vars7))
val vars8: Set<Any> = setOf(25,50,75,100,125,150,"arun","kumar","sasi","jai")
println("Thank you users for spending the time with our application")
for(vars9 in vars8){
println(vars8)
}
println("print vars8.elementAt(4)")
println(vars8.elementAt(3))
println("print vars8.elementAtOrNull(5)")
println(vars8.elementAtOrNull(5))
}
Output:
In the second example, we used the collection’s concept along with the const values.
Example #3
Code:
const val str = "It is the basic const type value"
fun main(args:Array<String>) {
println("Welocme To My Domain is the third example that related to the kotlin const concepts")
println(str)
}
Output:
In the final example, we used the const keyword used and printed it on the main method.
Conclusion
The kotlin language uses many type like const, static, final keywords, each of the type having different features along with the separate logic behind with the code. For example, in const keyword does not change the values; it is also used to the main method for printing the values on the console screen.
Recommended Articles
This is a guide to Kotlin const. Here we discuss the introduction, syntax, and working of const in Kotlin along with different examples for better understanding. You may also have a look at the following articles to learn more –