var and val are both used to declare variables in Kotlin language. However, there are some key differences between them:
It is a general variable. The value of a variable that is declared using var can be changed anytime throughout the program. var is also called mutable and non-final variable, as there value can be changed anytime.
Previous marks is 10 New marks 30
The object stored using val cannot be changed, it cannot be reassigned, it is just like the final keyword in java. val is immutable. Once assigned the val becomes read-only, however, the properties of a val object could be changed, but the object itself is read-only.
Val cannot be reassigned
Book(name=Java, price=1000) Book(name=Kotlin, price=1000)
- Difference between Kotlin and Scala
- Difference between Java and Kotlin in Android with Examples
- Kotlin Exception Handling | try, catch, throw and finally
- Kotlin Nested class and Inner class
- Kotlin Expression, Statement and Block
- Kotlin | Default and Named argument
- Kotlin | Lambdas Expressions and Anonymous Functions
- Kotlin Class and Objects
- Kotlin Setters and Getters
- Kotlin Nested try block and multiple catch block
- Kotlin | Type Checking and Smart Casting
- Kotlin | Class Properties and Custom Accessors
- Kotlin | Plus and minus Operators
- How to Implement Tabs, ViewPager and Fragment in Android using Kotlin?
- Kotlin Data Types
- Hello World program in Kotlin
- Kotlin | Retrieve Collection Parts
- Destructuring Declarations in Kotlin
- DatePicker in Kotlin
- Kotlin labeled continue
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.