We discuss null safety in Kotlin language. Kotlin treats nullable and non-nullable references differently. ie., You cannot assign null to references. Compiler doesn’t like it. If you have a reference that can accept null value too, then the question-mark(?) comes into picture. Null comparisons have also been made simple thanks to Elvis operator (?:)
The examples used in the video are given below
fun main(args:Array<String>){
var name:String = "Ram"
//name = null
//This condition is always true
if(name != null){
println(name)
}
var anotherName:String? = "John"
if(anotherName != null){
println(anotherName)
}
anotherName = null
println(anotherName)
println(anotherName?.length ?: -1)
}
Nullable types can be passed to functions and also returned as output
fun calculateLength(value:String?):Int?{
return value?.length
}
fun main(args:Array<String>){
println(calculateLength("Joe"))
println(calculateLength(null))
}
Here’s the 3 part videos on implementing a circuit breaker pattern in Spring Boot applications using Netflix Hystrix library. The first part is a short introduction and the other two parts show a code-based demo with a light music in the background. Enjoy!!!
fun main(args:Array<String>){
val range = 1 .. 10
println(range.javaClass)//IntRange
val rangeWithStep = 1 .. 10 step 2
println(rangeWithStep.first)
println(rangeWithStep.last)
println(rangeWithStep.step)
val range2 = 1 until 10
val range3 = 10 downTo 1 step 2
println(range3.first)
println(range3.last)
}
range can be declared using .. operator or using until and downTo keywords with an option incremental step. In the example above range is an instance of IntRange class. So, we can use properties like first, last, step etc., We can also use methods like min(), max(), average(), filter(), map(), reduce() etc.,
fun print(message:String){
val range = 1 .. 10
println(range.max())
println(range.min())
println(range.average())
println(range.filter { num -> num % 2 == 0 })
println(range.map { num -> num * num })
println(range.reduce { total, num -> total + num })
}