variables and types

Read only by default

val popcorn = 5
var customers = 10
customers = 8

val cannot be reassigned var can The default is read only because most variables do not need to change

Basic types

Int , Long , Float , Double , Boolean , Char , String Kotlin infers the type from the value No primitive versus object split at the source level

Null safety

var name: String? = null

The ? makes the type nullable A plain String cannot hold null That single difference removes a whole class of runtime failures

Safe calls

val length = name?.length

If name is null, the expression is null No exception That is the feature

Elvis operator

val result = name ?: "unknown"

Use a fallback when the value is null It reads better than a ternary for this case

Next

Null safety in depth in 03 null safety

Test this in your lab first

Posted