Skip to content

Commit 660e7d9

Browse files
committed
Null Safety, LateInit and Lazy Keyword
1 parent e876c08 commit 660e7d9

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

src/46_null_safety.kt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
fun main(args: Array<String>) {
3+
4+
// WAP to find out length of name
5+
val name: String? = "Steve" // change it to null and see the effect in output
6+
7+
// 1. Safe Call ( ?. )
8+
// Returns the length if 'name' is not null else returns NULL
9+
// Use it if you don't mind getting NULL value
10+
println("The length of name is ${name?.length}")
11+
12+
13+
// 2. Safe Call with let ( ?.let )
14+
// It executes the block ONLY IF name is NOT NULL
15+
name?.let {
16+
println("The length of name is ${name.length}")
17+
}
18+
19+
20+
// 3. Elvis-operator ( ?: )
21+
// When we have nullable reference 'name', we can say "is name is not null", use it,
22+
// otherwise use some non-null value"
23+
val len = if (name != null)
24+
name.length
25+
else
26+
-1
27+
28+
val length = name?.length ?: -1
29+
println("The length of name is ${length}")
30+
31+
// 4. Non-null assertion operator ( !! )
32+
// Use it when you are sure the value is NOT NULL
33+
// Throws NullPointerException if the value is found to be NULL
34+
35+
println("The length of name is ${name!!.length}")
36+
}

src/47_lateinit_keyword.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
3+
fun main(args: Array<String>) {
4+
5+
val country = Country()
6+
7+
// country.name = "India"
8+
// println(country.name)
9+
10+
country.setup()
11+
}
12+
13+
class Country {
14+
15+
lateinit var name: String
16+
17+
fun setup() {
18+
name = "USA"
19+
println("The name of country is $name")
20+
}
21+
}
22+
23+
// lateinit used only with mutable data type [ var ]
24+
// lateinit used only with non-nullable data type
25+
// lateinit values must be initialised before you use it
26+
27+
// If you try to access lateinit variable without initializing it then it throws UninitializedPropertyAccessException

src/48_lazy_keyword.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
3+
val pi: Float by lazy {
4+
3.14f
5+
}
6+
7+
fun main(args: Array<String>) {
8+
9+
println("Some initial code.....")
10+
11+
// pi is not initialised yet
12+
13+
val area1 = pi * 4 * 4 // pi gets initialised and assigned the value of 3.14f for the first time
14+
15+
val area2 = pi * 9 * 9 // The value pi is loaded from cache memory
16+
17+
println("Some more code....")
18+
}
19+
20+
21+
// ‘lazy initialization’ was designed to prevent unnecessary initialization of objects.
22+
// You variables will not be initialised unless you use it in your code
23+
// It is initialized only once. Next time when you use it, you get the value from cache memory.
24+
25+
// It is thread safe
26+
// It is initialized in the thread where it is used for the first time.
27+
// Other threads use the same value stored in the cache
28+
29+
// The variable can be var or val.
30+
// The variable can be nullable or non-nullable

0 commit comments

Comments
 (0)