forked from smartherd/KotlinTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Null Safety, LateInit and Lazy Keyword
- Loading branch information
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
fun main(args: Array<String>) { | ||
|
||
// WAP to find out length of name | ||
val name: String? = "Steve" // change it to null and see the effect in output | ||
|
||
// 1. Safe Call ( ?. ) | ||
// Returns the length if 'name' is not null else returns NULL | ||
// Use it if you don't mind getting NULL value | ||
println("The length of name is ${name?.length}") | ||
|
||
|
||
// 2. Safe Call with let ( ?.let ) | ||
// It executes the block ONLY IF name is NOT NULL | ||
name?.let { | ||
println("The length of name is ${name.length}") | ||
} | ||
|
||
|
||
// 3. Elvis-operator ( ?: ) | ||
// When we have nullable reference 'name', we can say "is name is not null", use it, | ||
// otherwise use some non-null value" | ||
val len = if (name != null) | ||
name.length | ||
else | ||
-1 | ||
|
||
val length = name?.length ?: -1 | ||
println("The length of name is ${length}") | ||
|
||
// 4. Non-null assertion operator ( !! ) | ||
// Use it when you are sure the value is NOT NULL | ||
// Throws NullPointerException if the value is found to be NULL | ||
|
||
println("The length of name is ${name!!.length}") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
|
||
fun main(args: Array<String>) { | ||
|
||
val country = Country() | ||
|
||
// country.name = "India" | ||
// println(country.name) | ||
|
||
country.setup() | ||
} | ||
|
||
class Country { | ||
|
||
lateinit var name: String | ||
|
||
fun setup() { | ||
name = "USA" | ||
println("The name of country is $name") | ||
} | ||
} | ||
|
||
// lateinit used only with mutable data type [ var ] | ||
// lateinit used only with non-nullable data type | ||
// lateinit values must be initialised before you use it | ||
|
||
// If you try to access lateinit variable without initializing it then it throws UninitializedPropertyAccessException |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
|
||
val pi: Float by lazy { | ||
3.14f | ||
} | ||
|
||
fun main(args: Array<String>) { | ||
|
||
println("Some initial code.....") | ||
|
||
// pi is not initialised yet | ||
|
||
val area1 = pi * 4 * 4 // pi gets initialised and assigned the value of 3.14f for the first time | ||
|
||
val area2 = pi * 9 * 9 // The value pi is loaded from cache memory | ||
|
||
println("Some more code....") | ||
} | ||
|
||
|
||
// ‘lazy initialization’ was designed to prevent unnecessary initialization of objects. | ||
// You variables will not be initialised unless you use it in your code | ||
// It is initialized only once. Next time when you use it, you get the value from cache memory. | ||
|
||
// It is thread safe | ||
// It is initialized in the thread where it is used for the first time. | ||
// Other threads use the same value stored in the cache | ||
|
||
// The variable can be var or val. | ||
// The variable can be nullable or non-nullable |