NULLABLES, ELVIS Operator and NOT NULL Assertion in Android KOTLIN

Pubblicato il: 01 gennaio 1970
sul canale di: tutorialsEU - Android
875
28

Did you know that there was a time before the NULL reference? Let me help you to get a better understanding on Android KOTLIN by teaching you about NULLABLES, ELVIS Operator and NOT NULL Assertion! Want to learn everything about Kotlin? Get this!    • Android Complete Course  

Here are the links by the way!

Free download. https://page.tutorials.eu/android

Our Android Masterclass. https://bit.ly/3l6vG86

We´ll make sure to make a Developer out of you In no time!

☕ You can also support us now by buying us a coffee! ☕ https://www.buymeacoffee.com/tutorialsEU

And we promise to create more cool content for you!

Null Safety in Kotlin

Nullable types and non-null types
Kotlin's type system is aimed at eliminating the danger of null references, also known as The Billion Dollar Mistake.

One of the most common pitfalls in many programming languages, including Java, is that accessing a member of a null reference will result in a null reference exception. In Java this would be the equivalent of a NullPointerException, or an NPE for short.

The only possible causes of an NPE in Kotlin are:

· An explicit call to throw NullPointerException().

· Usage of the !! operator that is described below.

· Data inconsistency with regard to initialization, such as when:

An uninitialized this available in a constructor is passed and used somewhere (a "leaking this").

A superclass constructor calls an open member whose implementation in the derived class uses an uninitialized state.

Java interoperation:

Attempts to access a member of a null reference of a platform type;

Nullability issues with generic types being used for Java interoperation. For example, a piece of Java code might add null into a Kotlin MutableList String, therefore requiring a MutableList String? for working with it.

Other issues caused by external Java code.

In Kotlin, the type system distinguishes between references that can hold null (nullable references) and those that cannot (non-null references). For example, a regular variable of type String cannot hold null

Now, if you call a method or access a property on a, it's guaranteed not to cause an NPE.

But if you want to access the same property on b, that would not be safe, and the compiler reports an error.

But you still need to access that property, right? There are a few ways to do so.

Checking for null in conditions
First, you can explicitly check whether b is null, and handle the two options separately.

The compiler tracks the information about the check you performed, and allows the call to length inside the if. More complex conditions are supported as well

Safe calls
Your second option for accessing a property on a nullable variable is using the safe call operator ?.

This returns b.length if b is not null, and null otherwise. The type of this expression is Int?.

Such a chain returns null if any of the properties in it is null.

To perform a certain operation only for non-null values, you can use the safe call operator together with let

A safe call can also be placed on the left side of an assignment. Then, if one of the receivers in the safe calls chain is null, the assignment is skipped and the expression on the right is not evaluated at all

Elvis operator
When you have a nullable reference, b, you can say "if b is not null, use it, otherwise use some non-null value":

Instead of writing the complete if expression, you can also express this with the Elvis operator ?:

If the expression to the left of ?: is not null, the Elvis operator returns it, otherwise it returns the expression to the right. Note that the expression on the right-hand side is evaluated only if the left-hand side is null.

The !! operator
The third option is for NPE-lovers: the not-null assertion operator (!!) converts any value to a non-null type and throws an exception if the value is null. You can write b!!, and this will return a non-null value of b (for example, a String in our example) or throw an NPE if b is null

Thus, if you want an NPE, you can have it, but you have to ask for it explicitly and it won’t appear out of the blue.

Check out the video if you want to learn more!

#code #android #kotlin #programming #null #nullable #elvis #notnull

tutorialsEU offers you free video tutorials about programming and development for complete beginners up to experienced programmers.
This includes C#, Unity, Python, Android, Kotlin, Machine Learning, etc.
Stay tuned and subscribe to tutorialsEU: https://goo.gl/rBFh3x

Android:    / @tutorialseuandroid  
C#:    / @tutorialseuc  
Unity:    / @tutorialseuunity  
Facebook:   / tutorialseu-109380204093233  
LinkedIn:   / tutorialseu  
Discord:   / discord  


In questa pagina del sito puoi guardare il video online NULLABLES, ELVIS Operator and NOT NULL Assertion in Android KOTLIN della durata di ore minuti seconda in buona qualità , che l'utente ha caricato tutorialsEU - Android 01 gennaio 1970, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 875 volte e gli è piaciuto 28 spettatori. Buona visione!