0

kotlin beginner question.

I dont understand why this code wont work. var input: String = readLine();

14th Nov 2021, 4:29 PM
ACE
ACE - avatar
3 ответов
+ 3
This is because of the null savety of kotlin. You can take this input on 2 ways: 1. Like A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ says var input: String = readLine()!! This both !! at the end prevent any NULL values for input. Here you CAN NOT input an empty line. This will thrown an runtime error. But for the compiler it's clear, that input is surely non NULL for the whole code. 2. Way: var input: String? = readLine() This ? after the type says, that this variable CAN be NULL. So here you can make an empty input (that's NULL). But now you have to do a NULL-check for input, for each operation which not allow NULL values. In the whole code. Otherwise you will get an compiler error. https://agrawalsuneet.github.io/blogs/safe-calls-vs-null-checks-in-kotlin/
15th Nov 2021, 2:53 PM
Coding Cat
Coding Cat - avatar
+ 1
【ACE】 You have to write readLine()!! var input:String = readLine()!!;
14th Nov 2021, 11:09 PM
A͢J
A͢J - avatar