Skip to content

Commit

Permalink
Null Safety, LateInit and Lazy Keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
smartherd committed Sep 19, 2018
1 parent e876c08 commit 660e7d9
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/46_null_safety.kt
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}")
}
27 changes: 27 additions & 0 deletions src/47_lateinit_keyword.kt
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
30 changes: 30 additions & 0 deletions src/48_lazy_keyword.kt
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

0 comments on commit 660e7d9

Please sign in to comment.