{"id":531,"date":"2018-10-29T16:06:52","date_gmt":"2018-10-29T15:06:52","guid":{"rendered":"https:\/\/www.scionova.com\/?p=531"},"modified":"2018-10-29T16:06:52","modified_gmt":"2018-10-29T15:06:52","slug":"introduction-to-kotlin","status":"publish","type":"post","link":"https:\/\/www.scionova.com\/en\/introduction-to-kotlin\/","title":{"rendered":"Introduction to Kotlin"},"content":{"rendered":"<p>In this blog post I will give you a small introduction to the programming language Kotlin (which last year was added as a supported language for Android) and describe a few of my favorite features of the language.<\/p>\n<h3><strong>Background<\/strong><\/h3>\n<p>Last year at Google I\/O, it was announced that Kotlin would be added as supported language in Android. A lot of tutorials, introductions and examples has emerged from the community since, and it is clear from <a href=\"https:\/\/developer.android.com\/kotlin\/\">the Android Developer page for Kotlin<\/a> that Google themselves want developers to see the advantages of Kotlin with a lot of helpful documentation and examples.<\/p>\n<p>Android is growing rapidly in the automotive industry, among others <a href=\"https:\/\/group.volvocars.com\/company\/innovation\/android\">Volvo<\/a> and <a href=\"https:\/\/www.engadget.com\/2017\/05\/15\/audi-and-volvo-go-all-in-on-android-auto\/\">Audi<\/a> has decided to use the Android operating system in their infotainment systems. This gives for a perfect reason to get some more knowledge about this rising star of programming languages!<\/p>\n<p>In Android, Kotlin is compiled to JVM byte code so it is possible to call Java code from Kotlin and vice versa, which makes it easy to have a gradual transition from Java to Kotlin in an application. Kotlin is supported from Android Studio 3.0, which even introduced <a href=\"https:\/\/developer.android.com\/studio\/projects\/add-kotlin#convert-to-kotlin-code\">a feature to convert a file containing Java code to Kotlin<\/a>. In my experience this function doesn\u2019t work optimally but it gives you a head start on your way to update your application to Kotlin.<\/p>\n<h3><strong>Nullability<\/strong><\/h3>\n<p>In Kotlin, the <a href=\"https:\/\/www.infoq.com\/presentations\/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare\">&#8220;billion dollar mistake&#8221;<\/a> has been solved by forcing the programmer to express nullability explicitly in the type of a variable. By appending a question mark to the type, we let the compiler (and other programmers) know that the variable is nullable.<\/p>\n<p>When we try to assign null to a variable of non-nullable type:<br \/>\n<script src=\"https:\/\/gist.github.com\/johannesjansson\/72a6c1c79b7925b1c1d142fffccb6d01.js\"><\/script><\/p>\n<p>After appending the question mark, it is possible for the variable to hold null:<br \/>\n<script src=\"https:\/\/gist.github.com\/johannesjansson\/0cf095c143a5705a47b5ae462f886196.js\"><\/script><\/p>\n<p><i>Safe call operator<\/i><br \/>\nIn order to call a member function on a variable of nullable type, the safe call operator (<span style=\"font-family: courier;\">?.<\/span>) is one of the possible ways. If we try to call a member function the usual way, we get an error:<br \/>\n<script src=\"https:\/\/gist.github.com\/johannesjansson\/efe7c81a5688c173e9bfc5cb50cbce61.js\"><\/script><\/p>\n<p>Though if we use the safe call operator, we get a different result:<br \/>\n<script src=\"https:\/\/gist.github.com\/johannesjansson\/b501fd1c70fa985f12b27071ab55bdcf.js\"><\/script><\/p>\n<p>As you can see, the safe call operator returns null if the variable it is applied to holds a null reference. The returned value will also be a nullable, in this case an <span style=\"font-family: courier;\">Int?<\/span>.<\/p>\n<p><i>Elvis operator<\/i><br \/>\nAnother handy feature of Kotlin is the so-called Elvis operator (<span style=\"font-family: courier;\">?:<\/span>). It returns the value to the left of the operator if it is not null, and the value to the right if it is.<br \/>\n<script src=\"https:\/\/gist.github.com\/johannesjansson\/616e798366d35e4572e87725f9634ff2.js\"><\/script><\/p>\n<p><i>Continued reading about Kotlin null safety<\/i><br \/>\nFor more information regarding null safety in Kotlin, check out <a href=\"https:\/\/kotlinlang.org\/docs\/reference\/null-safety.html\">the official documentation on the subject<\/a>.<\/p>\n<h3><strong>Mutability of variables<\/strong><\/h3>\n<p>As you might have noticed in the examples above, a variable can be declared with the keyword <span style=\"font-family: courier;\">var<\/span>.<\/p>\n<p>This creates a variable that is mutable, e.g. it is possible to alter its value at a later point. It is also possible to make a variable immutable, this is done using the keyword <span style=\"font-family: courier;\">val<\/span> when declaring the variable instead:<br \/>\n<script src=\"https:\/\/gist.github.com\/johannesjansson\/a45ec43d67f8894c71be0b4eb6fa6256.js\"><\/script><\/p>\n<h3><strong>Extension functions<\/strong><\/h3>\n<p>In Kotlin, it is possible to extend classes with your own functions without creating subclasses. This concept is called extension functions and they are declared like this:<br \/>\n<script src=\"https:\/\/gist.github.com\/johannesjansson\/ccff0a1b094226207302f0aac9fa0713.js\"><\/script><\/p>\n<p>In this example the standard class Int is extended with a function square that can be called on integers in all files the declaration of the extension is included in. See <a href=\"https:\/\/kotlinlang.org\/docs\/reference\/extensions.html\">the official documentation<\/a> for more examples.<\/p>\n<h3><strong>Data classes<\/strong><\/h3>\n<p>What in Java is called a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Plain_old_Java_object\">POJO<\/a>, has its Kotlin counterpart\u00a0in data classes. A data class contains a lot of extra functionality which would be boilerplate in a POJO, e.g. equals, hashCode and toString. This allows you to create a\u00a0class with just a one-liner:<br \/>\n<script src=\"https:\/\/gist.github.com\/johannesjansson\/0d4a9b876fdf644e0c20b432a2ade4ef.js\"><\/script><\/p>\n<p>It is possible to declare a variable with either <span style=\"font-family: courier;\">var<\/span> or\u00a0<span style=\"font-family: courier;\">val<\/span> in a class, with the difference that a <span style=\"font-family: courier;\">var<\/span> makes the compiler to generate a setter function for the variable in the class beside the getter that is generated for public variables.<\/p>\n<h3><strong>Finishing words<\/strong><\/h3>\n<p>The null safety of Kotlin is a big selling point of Kotlin and something that often is pointed out as an advantage of using Kotlin over Java when developing for Android (beside for verbosity). In this post I have scarcely scratched the surface of Kotlin and if you are interested in learning more I would recommend you to continue your reading on <a href=\"https:\/\/kotlinlang.org\/\">the official webpage of the language<\/a>, but also on <a href=\"https:\/\/developer.android.com\/kotlin\/\">the Android Developer page for Kotlin<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-65 alignnone\" src=\"https:\/\/scionova.evdy.se\/wp-content\/uploads\/2018\/10\/Johannes-200x300.jpg\" alt=\"\" width=\"200\" height=\"300\" \/><\/p>\n<p>\/\/ Johannes Jansson, Software Developer<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog post I will give you a small introduction to the programming language Kotlin (which last year was added as a supported language for Android) and describe a few of my favorite features of the language. Background Last year at Google I\/O, it was announced that Kotlin would be added as supported language [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":""},"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/www.scionova.com\/en\/wp-json\/wp\/v2\/posts\/531"}],"collection":[{"href":"https:\/\/www.scionova.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.scionova.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.scionova.com\/en\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.scionova.com\/en\/wp-json\/wp\/v2\/comments?post=531"}],"version-history":[{"count":0,"href":"https:\/\/www.scionova.com\/en\/wp-json\/wp\/v2\/posts\/531\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.scionova.com\/en\/wp-json\/wp\/v2\/media?parent=531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.scionova.com\/en\/wp-json\/wp\/v2\/categories?post=531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.scionova.com\/en\/wp-json\/wp\/v2\/tags?post=531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}