1
votes

I really want to know why Kotlin use the word val to stand for constant?

If var means variable.val means what ? val means var + l or val is only one word for short?

In the Kotlin Documentation,we only have the following words.

Read-only local variables are declared using val keyword. Mutable local variables are declared using var keyword.

This question is not only to make sense of the word val,but also Remind the WORD MAKER to tell us why they name the word,this is not a little thing,this will make us more comfortable to learn the new knowledge,we want to learn and make sense of everything.

For example,many people want to know why Swift use the word let or Why Objective-C use the .m filename extension?

I think the official documentation or response is really important,any guess or assuming is not really helpful,because it's not convictive,the confused will be still the confused.

I also asked this question in the official forum:
https://discuss.kotlinlang.org/t/why-kotlin-use-the-word-val-to-stand-for-constant/4491

Some references:
https://discuss.kotlinlang.org/t/change-val-to-something-else/1180/13

4
If you are asking for official explanation. Kotlin forum may be a more suitable place to ask.BakaWaii
OK,I will try it later.Thank you for you help!ifeegoo
They might've taken those keywords from Scala since Scala uses them the same way.Dan W
Yeah,this is a good guess.Just now I have found this:discuss.kotlinlang.org/t/change-val-to-something-else/1180/13 Someone mentioned Scalaifeegoo

4 Answers

9
votes

val means value

This is a read-only value.

var is a mutable value

const would on the other hand be not 100% correct. The value PI (3.14..) is a constant. Its value never changes. The value of x in this line val x = random.nextInt() will (hopefully) always be different, but you want the value not to be modified in the function. So the keyword val is quite appropriate.

2
votes

val keyword is only one word. abbreviation for val not found anywhere. Your question explains what is val.

Read-only local variables are declared using val keyword. Mutable local variables are declared using var keyword.

Here is a site explains why val is Read-only and not immutable.

http://blog.danlew.net/2017/05/30/mutable-vals-in-kotlin/

val does not mean immutable, val means read-only. That means that you're not allowed to explicitly write to a val, but it doesn't guarantee that they're immutable

https://artemzin.com/blog/kotlin-val-does-not-mean-immutable-it-just-means-readonly-yeah/

Kotlin allows you declare get() of the val which breaks immutability of the property and leaves only read permission for external "users".

1
votes

Kotlin's syntax is inspired by Scala. In Kotlin several ideas are taken from Scala besides the syntax. Kotlin also adds things on its own and does certain things very differently than Scala (i.e., no implicit conversion in Kotlin compared to Scala). Long matter short: You have to ask the Scala guys why they chose the keyword combination var and val.

0
votes
  • val from value.
  • var from variable.

  • value - a property such as number assigned to or calculated for a variable, constant or expression(wiki)

  • variable - a symbolic name associated with a value and whose associated value may be changed(wiki)