0

kotlin beginner question.

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

14th Nov 2021, 4:29 PM
ļ¼”ļ¼£ļ¼„
ļ¼”ļ¼£ļ¼„ - avatar
3 Answers
+ 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
怐ļ»æļ¼”ļ¼£ļ¼„怑 You have to write readLine()!! var input:String = readLine()!!;
14th Nov 2021, 11:09 PM
AĶ¢J
AĶ¢J - avatar