Why have nullable types?
A nullable type is one that allows null values. Unlike other programming languages, Kotlin tracks values that can be null to stop you from performing invalid actions on them. Performing invalid actions on null values is the most common cause of runtime problems in other languages such as Java, and can cause your application to crash in a heap when you least expect it. These problems, however, rarely happen in Kotlin because of its clever use of nullable types.
You declare that a type is nullable by adding a question mark (?) to the end of the type.
What happens if I initialize a variable with a null value, and let the compiler infer the variable’s type?
The compiler sees that the variable needs to be able to hold null values, but as it has no information about any other kinds of object it might need to hold, it creates a variable that can only hold a value of null. This probably isn’t what you want, so if you’re going to initialize a variable with a null value, make sure you specify its type.
Keep things safe with safe calls
A safe call lets you access functions and properties in a single operation without you having to perform a separate null-check.
?. is the safe call operator. It lets you safely access a nullable type’s functions and properties.
You can chain safe calls together
Another advantage of using safe calls is that you can chain them together to form expressions that are powerful yet concise.
Telegram Group: https://t.me/androidNinjaWarrior
Guide to watch:
00:30 Why have nullable types in Kotlin?
03:00 How to create null type variable in kotlin?
07:50 How to create null parameter in functions?
10:27 How to create null return type in functions?
11:48 How to create array of Null in kotlin?
15:50 How to create null object?
20:00 Safe call in Kotlin
26:23 How to handle null using let scope function?
30:25 How to use Elvis operator in null?
34:12 Non-null assertion in Kotlin
#Null #NullSafety #Nulls #NullInKotlin #SafeCall #HandleNull #Kotlin #KotlinNull #KotlinNullSafety
Disclaimer- Some content are used for educational purposes under fair use.
Copyright Disclaimer Under Section 107 of the Copyright Act 1976, allowance is made for "fair use" for purposes such as criticism, comment, news reporting, teaching, scholarship, and research. Fair use is a use permitted by copyright statute that might otherwise be infringing. Non-profit, educational, or personal use tips the balance in favor of fair use. All credit for copyright material used in the video goes to the respected Owner.
Thank you guys.