kotlin-null-safety

What is the Kotlin double-bang (!!) operator?

冷暖自知 提交于 2019-11-27 19:12:27
I'm converting Java to Kotlin with Android Studio. I get double bang after the instance variable. What is the double bang and more importantly where is this documented? mMap!!.addMarker(MarkerOptions().position(london).title("Marker in London")) hotkey This is unsafe nullable type ( T? ) conversion to a non-nullable type ( T ). It will throw NullPointerException if the value is null . It is documented here along with Kotlin means of null-safety. Alf Moh Here is an example to make things clearer. Say you have this function fun main(args: Array<String>) { var email: String email = null println

Best way to null check in Kotlin?

巧了我就是萌 提交于 2019-11-27 11:37:44
问题 Should I use double = , or triple = ? if(a === null) { //do something } or if(a == null) { //do something } Similarly for 'not equals': if(a !== null) { //do something } or if(a != null) { //do something } 回答1: Both approaches generate the same bytecode so you can choose whatever you prefer. 回答2: A structural equality a == b is translated to a?.equals(b) ?: (b === null) Therefore when comparing to null , the structural equality a == null is translated to a referential equality a === null .

What is the Kotlin double-bang (!!) operator?

北城余情 提交于 2019-11-26 19:21:19
问题 I'm converting Java to Kotlin with Android Studio. I get double bang after the instance variable. What is the double bang and more importantly where is this documented? mMap!!.addMarker(MarkerOptions().position(london).title("Marker in London")) 回答1: This is unsafe nullable type ( T? ) conversion to a non-nullable type ( T ). It will throw NullPointerException if the value is null . It is documented here along with Kotlin means of null-safety. 回答2: Here is an example to make things clearer.