Programming with Kotlin null safety lesson 4

in #programming7 years ago (edited)

![Kotlin-logo.png]()

<h2><strong>   <h2><strong> In the previous lesson we talked about the definition of variables in kotlin and assigning a value to it But what if we wanted to define  a variable without assigning any value to it during the definition? We can do this in the following way: <h2><strong> 1-var OR val  <h2><code><strong>2- its name  <h2><code><strong>3-the tag:  <h2><code><strong>4- its type  <h2><code><strong>5-add a question mark ? <h2><strong>The question mark means that the value will be assigned later <h2><strong> EX: <h2><strong> var name:String? <h2><strong> EX: <h2><strong> fun main (args:Araay <String>) <h2><strong> {  <h2><strong>        var name:String? <h2><strong>       name = "mark" <h2><strong>       print(name) <h2><strong>        name = null <h2><strong>         print(name!!)  <h2><strong>}<br /> In this example, we defined a variable called name its type is string, then we assigned the "mark" value, and then ordered it to print on the screen.  <h2><strong>Then we assigned it a null value, and also print on the screen <h2><strong>  But the result of the program will be  (mark) only  because of the use of exclamation marks in the second print  print(name !!) <h2><strong>  which means Do not execute if the value is null  <h2><strong> This process is called null safety