Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Beyond Java - Kotlin in Android Development

Beyond Java - Kotlin in Android Development

Short introduction to Kotlin programming langugage. Presentation prepared for Poznan Android Developer Group meetup.

Sample project code: https://github.com/mrmike/kotlin-loader-sample

Michal Moczulski

October 02, 2014
Tweet

More Decks by Michal Moczulski

Other Decks in Programming

Transcript

  1. Introduction • Created by JetBrains (July 2011) • Statically typed

    • Runs on JVM • Fully interoperable with Java • Can be compiled to JavaScript too
  2. Syntax package foo.bar fun main(args: Array<String>) { val foo =

    (1..100 step 3).filter { it % 2 == 0 } foo.indices.forEach { println("Value at position ${it} is ${foo.get(it)}") } }
  3. Object Oriented package foo.bar class Foo(val name: String) { fun

    sayHello() { println("Hello ${name}") } } fun main(args: Array<String>) { val instance = Foo("Kotlin") instance.sayHello() }
  4. When expression when (x) { 0 -> println("The answer is

    0") 42 -> println("The answer is 42") else -> println("Else block") }
  5. @break fun main(args: Array<String>) { @outer for (i in 1..100)

    { for (j in 100..1) { if (i < j) { break@outer } } } }
  6. Traits package foo.bar trait HelloTrait { val name: String fun

    sayHello() { println("Hello ${name}") } fun sayBye() { println("Bye") } } class Foo(override val name: String) : HelloTrait { // class methods } fun main(args: Array<String>) { val instance = Foo("Kotlin") instance.sayHello() instance.sayBye() }
  7. Data class - old way public class Person { public

    String name; public String surname; public int age; public Person(String name, String surname, int age) { this.name = name; this.surname = surname; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSurname() { return surname; } public void setSurname(String surname) { this.surname = surname; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
  8. Null safety var name : String = "Foo" name =

    null // Null can not be a value of a non- null type kotlin.String var numbers : Array<Int>? = array(1, 2, 3, 4, 5) numbers = null // That's ok numbers.size // (1) numbers?.size // (2) numbers!!.size // (3)
  9. Resources • http://kotlinlang.org/ • http://kotlinlang.org/docs/reference/ • http://kotlin-demo.jetbrains.com/ • http://blog.jetbrains.com/kotlin/ •

    https://github.com/jetbrains/kotlin • http://blog.jetbrains.com/kotlin/2013/08/working-with-kotlin- in-android-studio/