When we talk about null, we should also talk about Kotlin and its null-safety.
First of all, all types (Int, String, ...) are non-null by default. If you want to allow null for a certain variable, you use the nullable type (Int?, String?, ...).
When dealing with nullable variables, one can use the Elvis operator:
valname:String?=customer?.name
name will be either a String or null if customer or customer.name is null. It won't throw if customer is null.
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
When we talk about
null
, we should also talk about Kotlin and its null-safety.First of all, all types (
Int
,String
, ...) are non-null by default. If you want to allownull
for a certain variable, you use the nullable type (Int?
,String?
, ...).When dealing with nullable variables, one can use the Elvis operator:
name
will be either a String or null ifcustomer
orcustomer.name
isnull
. It won't throw ifcustomer
is null.Non-null by default is the way it should be. I have never used Kotlin, but it sounds like they are doing things right.
I also love the term Elvis Operator!