<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: golanir</title>
    <description>The latest articles on Forem by golanir (@golanir).</description>
    <link>https://forem.com/golanir</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F75393%2F20f6fbd6-4b15-4fb7-a714-1db581e69f58.jpeg</url>
      <title>Forem: golanir</title>
      <link>https://forem.com/golanir</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/golanir"/>
    <language>en</language>
    <item>
      <title>Kotlin 101</title>
      <dc:creator>golanir</dc:creator>
      <pubDate>Sun, 17 Mar 2019 23:16:25 +0000</pubDate>
      <link>https://forem.com/golanir/kotlin-101-420a</link>
      <guid>https://forem.com/golanir/kotlin-101-420a</guid>
      <description>&lt;p&gt;Kotlin is a (relatively) new statically typed language developed by Jetbrains, IntelliJ idea anyone?&lt;/p&gt;

&lt;p&gt;Open-sourced at 2011 and Kotlin 1.0 released on February 15th 2016.&lt;br&gt;
although widely used by JetBrains developers, Kotlin's big break was on Google I/O 2017 where Kotlin was announced as an official language for android development.&lt;/p&gt;

&lt;p&gt;Some of Kotlin's key features are:&lt;br&gt;
Concise, Safe, Interoperable &amp;amp; mature.&lt;/p&gt;

&lt;p&gt;Kotlin is 100% interoperable with Java and you can use it with your current project without any problems, you can call Java code from Kotlin and vice versa, all you need to do is add a small plugin and you're all set.&lt;/p&gt;

&lt;p&gt;Kotlin adds lots of modern features that organizations stuck with Java 7 or 8 or Android developers surely miss and one of its big advantages is that it solves the nullability problem.&lt;/p&gt;

&lt;p&gt;Kotlin has a place in any of today's development environments, Kotlin/JVM for Android, backend &amp;amp; everything else that runs on the JVM, Kotlin/JS for frontend developers and Kotlin/Native for iOS, MacOS, Windows, Linux, WebAssembly &amp;amp; Android (again).&lt;/p&gt;

&lt;p&gt;let's dive right in&lt;/p&gt;
&lt;h2&gt;
  
  
  VARIABLES
&lt;/h2&gt;

&lt;p&gt;in Kotlin we declare variables like this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;val&lt;/strong&gt; is a read only (immutable) variable that we must initialize at declaration and we can't change its value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;val name: String = "nir"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The compiler is smart enough to infer the type and we can declare it like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;val name = "nir"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;var&lt;/strong&gt; is a mutable variable&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var lastName: String = "golan"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As in val we can remove the type and our code will run just fine&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var lastName = "golan"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Variables declared with the var keyword can be change at a later time&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;lastName = "golanster"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;but we can't change the type, so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;lastName = 123
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;will not compile.&lt;/p&gt;

&lt;h2&gt;
  
  
  CLASSES
&lt;/h2&gt;

&lt;p&gt;Classes in kotlin are declared with the &lt;strong&gt;class&lt;/strong&gt; keyword&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Person constructor(val firstName: String, var lastName: String)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As in the variable declaration where we could omit the type, here we can omit the &lt;strong&gt;constructor&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Person (val firstName: String, var lastName: String)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;When we use the &lt;strong&gt;val&lt;/strong&gt; keyword in the constructor, we declare and assign the class properties.&lt;/p&gt;

&lt;p&gt;Another important class keyword is &lt;strong&gt;data&lt;/strong&gt;, when we add the &lt;strong&gt;data&lt;/strong&gt; keyword in front of the class we get getters, setters (for &lt;strong&gt;var&lt;/strong&gt; and not for &lt;strong&gt;val&lt;/strong&gt;) &amp;amp; toString, hashCode, equals &amp;amp; copy overrides.&lt;/p&gt;

&lt;h2&gt;
  
  
  Object &amp;amp; Class Instantiation
&lt;/h2&gt;

&lt;p&gt;In Kotlin we instantiate objects &amp;amp; classes like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;val person = Person("nir", "golan")
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Notice we don't use the &lt;strong&gt;new&lt;/strong&gt; keyword&lt;/p&gt;

&lt;h2&gt;
  
  
  Functions
&lt;/h2&gt;

&lt;p&gt;Functions in Kotlin are declared with the &lt;strong&gt;fun&lt;/strong&gt; keyword&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun sum(first: Int, second: Int) {}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If the function have a return value we add the return type &lt;strong&gt;after&lt;/strong&gt; the ()&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun sum(first: Int, second: Int): Int {}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;and in the {} we define the body of the function&lt;/p&gt;

&lt;h3&gt;
  
  
  if &amp;amp; when
&lt;/h3&gt;

&lt;p&gt;In kotlin if &amp;amp; when are expressions and have a return value, so in our function we can write&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun sum(first: Int, second: Int): Int {
    if (first &amp;gt; second) {
        first
    } else {
        second
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In Kotlin we also have &lt;strong&gt;default values&lt;/strong&gt; &amp;amp; &lt;strong&gt;named parameters&lt;/strong&gt; in functions&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun sum(first: Int = 5, second: Int = 10): Int {
    if (first &amp;gt; second) {
        first
    } else {
        second
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can call the function like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;val number = sum(5,10) //normal call
val number = sum(first = 5, second = 10) //named parameters
val number = sum(second = 5, first = 10) //with named parameters we can
                                         //call them in any order we want
val number = sum(second = 22) //with default values we can omit
                              //any parameter and call just the one we need
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And because Kotlin is a concise language we can omit some {} and get this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun sum(first: Int, second: Int) = if (first &amp;gt; second) first else second
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And this is the closest thing we have to a ternary operator in Kotlin.&lt;/p&gt;

&lt;p&gt;In Kotlin &lt;strong&gt;when&lt;/strong&gt; is a switch statement on steroids, it can be used as a normal switch statement, but because it's also an expression that returns a value when used like that, we &lt;strong&gt;must&lt;/strong&gt; add an else clause&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun whatAmI(value: Any?) = when(value) {
    3 -&amp;gt; "value is exactly 3"
    is Int -&amp;gt; "double the value = ${value * 2}"
    "What the fuck?" -&amp;gt; "Swich case + if statement!"
    else -&amp;gt; "No value"
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;In Kotlin everything inherits from Any, just like Object in Java&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the above use case we evaluate &lt;strong&gt;value&lt;/strong&gt;, we declare the value of &lt;strong&gt;value&lt;/strong&gt; and based on the value of &lt;strong&gt;value&lt;/strong&gt; we return the value :-)&lt;/p&gt;

&lt;h2&gt;
  
  
  Extension Function
&lt;/h2&gt;

&lt;p&gt;We can extend any Kotlin class with a function of our own, in fact, Kotlin itself is an Extension functions over Java.&lt;/p&gt;

&lt;p&gt;Let's take &lt;strong&gt;Int&lt;/strong&gt; for example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun Int.maxValue(other: Int) = if (this &amp;gt; other) this else other
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here we define an extension function for Int where we pass in another value and return the bigger one&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;val max = 3.maxValue(4) // returns 4
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can add the keyword &lt;strong&gt;infix&lt;/strong&gt; in front of the function thus making the syntax a bit more friendly&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;infix fun Int.maxValue(other: Int) = if (this &amp;gt; other) this else other
val max = 3 maxValue 4
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Null Safty
&lt;/h2&gt;

&lt;p&gt;First, let's see how NPE or null pointer exception can happen in Java&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;String luckyNumber = "7";
System.out.println(luckyNumber.length());
luckyNumber = null;
System.out.println(luckyNumber.length());
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here, when our program will reach the last line it will throw a &lt;code&gt;java.lang.NullPointerException&lt;/code&gt;, now lets see how Kotlin can help us with it.&lt;br&gt;
In Kotlin a variable &lt;strong&gt;can't&lt;/strong&gt; be null, if we declare a variable like we did before&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var name: String = null
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;our program will not compile and we will get this message: &lt;em&gt;null can not be a value of a non-null type String&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In order for us to use null in Kotlin we must declare a support for null in the type&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var name: String? = null
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;?&lt;/strong&gt; in the end of the type tells the compiler that &lt;code&gt;name&lt;/code&gt; can be null, if in the declaration we omit the type the compiler will infer the type as &lt;strong&gt;String?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When using nullable variables we have safe &amp;amp; unsafe ways to deal with them.&lt;br&gt;
First, let's take a look at the safe way,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;name?.length()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;?&lt;/strong&gt; will check if the variable is null or not and only if its not null will execute the &lt;code&gt;length()&lt;/code&gt; method&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;name!!.length()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;With &lt;strong&gt;!!&lt;/strong&gt; we're telling the compiler: "I know this can be null, trust me, i know what i'm doing".&lt;br&gt;
I don't have to tell you that this is the best way to get a NullPointerException, right?&lt;/p&gt;

&lt;p&gt;If we need to have a value returned even if the variable is null, we can use the elvis operator, which returns what's right of it if the left side is null&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;val res = name?.length() ?: 0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Collections
&lt;/h2&gt;

&lt;p&gt;In Kotlin we have all the basic collections: list, array, map, set, hashMap, hashSet, etc..&lt;br&gt;
Lets see some ways we can manipulate them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;val list = 1..100 // declaration of a list with the numbers 1 to 100

list.map { it * 2 } //map iterates over the list and **it**
                    //is the value currently being evaluated
    .filter { v -&amp;gt; v % 2 == 0 } //filter out the results that are odd
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now let's say that we have a list of 1000000000000000 elements, this could take time even on the latest system, and for the sake of the argument let's say we need to pass the list to a web page for it to display.&lt;br&gt;
We can't let the user wait several minutes until they see some info on the screen, this is where &lt;strong&gt;asSequence&lt;/strong&gt; comes in&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;val list = 1..1999999999999900

list.asSequence
    .map { it * 2 }
    .filter { v -&amp;gt; v % 2 == 0 }

list.forEach{print(it)} //extension function that iterate over the list
                        //and print the current value
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;asSequence&lt;/code&gt; evaluates the list as lazy and every time a new value is added it's being printed.&lt;/p&gt;

&lt;p&gt;That's all for now, Kotlin is a very exciting language with many more topics to cover and i will add more in the next few days.&lt;br&gt;
Happy coding.&lt;/p&gt;

</description>
      <category>kotlin</category>
    </item>
  </channel>
</rss>
