Is initialization necessary for val variables in Kotlin? | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 2

Is initialization necessary for val variables in Kotlin?

I know that the val variables can not change. This is similar to C++ constants. But I wonder if it's possible to declare a val without initialization. We're not "changing" it! We're just quantifying it separately from the declaration. I mean something like this val x:int ... x=1

3rd May 2021, 3:45 PM
Koorosh Nobakhtfar
Koorosh Nobakhtfar - avatar
2 Respuestas
+ 2
Yeah ofc, if you don't initalize it intellij won't even allow you to run through Also you can check it, you would get `blah` Is not initalized maybe if you have java background you may have confusion like ```java public String bruh ``` why this isn't possible in kotlin then well ```kt lateint var bruh: String ``` which will answer your question Koorosh Nobakhtfar
3rd May 2021, 7:07 PM
Ananiya Jemberu
Ananiya Jemberu - avatar
+ 3
Koorosh Nobakhtfar , i hope i understand you right. you can do like val x = 1 ... or you can do like this: val x1:Int = 1
3rd May 2021, 4:50 PM
Lothar
Lothar - avatar