Kotlin val vs var

텅텅텅·2023년 5월 27일
0

Kotlin

목록 보기
2/6

"val" and "var" are both used to declare variables, but they have different meanings and usage.

"val" is short for "value"

  • is used to declare an "immutable" variable, which cannot be reassigned onces it's initialized. (almost same as "final" in Java)
    ex1)
val name = "John"
//name = "Alice" <- error

"var" is short for "variables"

  • is used to declare a "mutable" variable, which can be reassigned after it's initialized.
var age = 35
age = 20
profile
아무것도모르오

0개의 댓글