{"id":3081,"date":"2020-01-22T15:42:07","date_gmt":"2020-01-22T20:42:07","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=3081"},"modified":"2020-01-23T09:46:39","modified_gmt":"2020-01-23T14:46:39","slug":"kotlin-how-to-understand-the-not-null-assertion-operator","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2020\/01\/22\/kotlin-how-to-understand-the-not-null-assertion-operator\/","title":{"rendered":"Kotlin: How to understand the not-null assertion operator (!!)"},"content":{"rendered":"\n<p>The Kotlin &#8220;not-null assertion operator&#8221; is expressed as two exclamation points. <code>!!<\/code>  that appear at the end of a variable name. For example: <code>myVariable!!<\/code><\/p>\n\n\n\n<p>Here is what the Kotlin documentation says about this operator:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"352\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/01\/image-8-1024x352.png\" alt=\"\" class=\"wp-image-3082\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/01\/image-8-1024x352.png 1024w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/01\/image-8-300x103.png 300w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/01\/image-8-768x264.png 768w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/01\/image-8-676x233.png 676w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2020\/01\/image-8.png 1470w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Here is what I learned about the not-null assertion operator after a little bit of experimentation:<\/p>\n\n\n\n<p> The difference between <code>b<\/code> and <code>b!!<\/code>:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">fun main() {\n    val b = null\n    \n    val n = b\n    println(\"n $n\")\n    \n    val l = b!!\n    println(\"l $l\")\n}<\/pre>\n\n\n\n<p>Yields:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">n null\nException in thread \"main\" kotlin.KotlinNullPointerException\n at FileKt.main (File.kt:12) \n at FileKt.main (File.kt:-1) <\/pre>\n\n\n\n<p><code>b<\/code> will print null<br><code>b!!<\/code> will yield an NPE, as seen above. <br><br>So the takeaway is: <\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>The non-null assertion operator can be used when we want to <em>force<\/em> a Null Pointer Exception when accessing a null variable.<\/strong><\/p><\/blockquote>\n\n\n\n<p>For more information about this and other Kotlin &#8220;null safety&#8221; operators, see:<br><a href=\"https:\/\/kotlinlang.org\/docs\/reference\/null-safety.html#the--operator\">https:\/\/kotlinlang.org\/docs\/reference\/null-safety.html#the&#8211;operator<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Kotlin &#8220;not-null assertion operator&#8221; is expressed as two exclamation points. !! that appear at the end of a variable name. For example: myVariable!! Here is what the Kotlin documentation says about this operator: Here is what I learned about the not-null assertion operator after a little bit of experimentation: The difference between b and &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2020\/01\/22\/kotlin-how-to-understand-the-not-null-assertion-operator\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Kotlin: How to understand the not-null assertion operator (!!)<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[214],"tags":[215],"class_list":["post-3081","post","type-post","status-publish","format-standard","hentry","category-kotlin","tag-kotlin"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/3081","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=3081"}],"version-history":[{"count":4,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/3081\/revisions"}],"predecessor-version":[{"id":3087,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/3081\/revisions\/3087"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=3081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=3081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=3081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}