What is the difference between const vs val?
Answer / Krishan Kant Sharma
In Kotlin, both 'const' and 'val' are used to declare constants. However, there is a significant difference between the two.nnVal is a constant variable that can be reassigned within its declaration if it is an object or a function reference. On the other hand, const is a compile-time constant, which means it cannot be reassigned at all, even if it's not an object or a function reference.nnFor example,nnval PI: Double = 3.14nncan be rewritten as:nnconst val PI: Double = 3.14nBut the following code will result in a compile-time error if PI is declared as const:nnconst val MY_OBJECT = SomeClass()nMY_OBJECT = AnotherClass()
| Is This Answer Correct ? | 0 Yes | 0 No |
How are Primary Constructors different from Secondary Constructors ?
What does "Null Safety" mean in Kotlin?
What are the features of java has that kotlin does not?
Why you should shift from Java to kotlin?
What is the difference between Return Type Unit and Return Type Nothing in Kotlin?
Why do we need write open before class name in Kotlin while working on inheritance?
How to make a class data class ?
Tell me the default behavior of Kotlin classes?
Who created Kotlin programming language and when?
What is Ranges operator in Kotlin?
How Kotlin works?
Define String datatype with example in Kotlin?