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 |
What is Range in Kotlin?
Can kotlin code be executed without jvm?
Describe the use of extension function?
Name the extension methods Kotlin provides to java.io.File?
What is Kotlin Native?
What are Data classes ? Aren’t they available in Java ?
What are the features available in kotlin but not in java?
Why is Kotlin interoperable with Java?
Is there any Ternary Conditional Operator in Kotlin like in Java?
What are the different types of constructors in Kotlin?
Explain functions in kotlin?
Where does the Kotlin run and what is the entry point of Kotlin?