乐闻世界logo
搜索文章和话题

What's the Difference Between var and val in Kotlin?

2月7日 11:54

In Kotlin, both var and val are used to declare variables, but the primary distinction lies in the mutability of the variables:

  • var: Declares a mutable variable, meaning its value can be modified after initialization.
  • val: Declares an immutable variable, also known as a read-only variable. Once initialized with an initial value, its value cannot be altered. In short, a variable defined with val is equivalent to a final variable in Java.
标签:Kotlin