Can anyone tell me difference between val and var in kotlin in simple term | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Can anyone tell me difference between val and var in kotlin in simple term

I tried to understand a lot. I try to understand this on other platforms too, i bought a book programing kotlin to understand this but i am not getting the point. i tried everything please tell me how to use it and where to use it in very simple language. example will be appreciated

6th Jul 2018, 3:16 PM
Goutam Malviya
2 Answers
6th Jul 2018, 3:18 PM
Mohammad Amir Aqeel
Mohammad Amir Aqeel - avatar
+ 9
Simply: var is used when the value is going to change. val when it isn't (constant.) Personally, until you understand it, code everything val. The compiler will complain, when it must be var and you should change it. Since the IntArray class doesn't change, the following is fine. val arr = arrayOf(1, 2) arr[0] = 3
6th Jul 2018, 3:33 PM
John Wells
John Wells - avatar