Kotlin is a statically typed programming language for the JVM, Android and the browser.
More and more in the Android community start using Kotlin, we’ll see why and how you can do it too.
Java is great
▣ Very popular.
▣ Great community.
▣ Lots of frameworks and libraries.
▣ Supported by big companies (Oracle &
Google).
▣ Portable, fast, well documented.
▣ Great IDEs
▣ You can write server side, web and mobile.
Yes but...
▣ Lot’s of boilerplate
▣ Null checks
▣ Exception handling
▣ Function pointers
▣ Data classes (POJO)
▣ Extension functions
▣ Operator overloading
▣ String interpolation
▣ And more...
On Android it even worse
▣ Barely support Java 8 (N+)
▣ Partially support Java 7 (KitKat+)
▣ There are some libraries that fill some of the
missing features (retro lambda, RX, etc..) but
it’s not the same.
▣ It mainly feel too ‘heavy’ for front end code.
Kotlin?
▣ Open source, lead by JetBrains.
▣ JVM language (Like Scala, Groovy and Clojure)
▣ Can call Java code and vice versa.
▣ Fully integrated IDE.
▣ Small (600KB before ProGuard).
▣ Statically typed so no runtime overhead.
▣ Used by a lot of big companies, even by the
Android team (data binding).
when (x) {
in 1..10 -> print("x is in the range")
in validNumbers -> print("x is valid")
!in 10..20 -> print("x is outside the range")
else -> print("none of the above")
}
fun mul(x: Int, y: Int = 2): Int {
return x * y
}
fun mul(x: Int, y: Int = 2) = x * y
view.setOnClickListener({ //click handling })
if (obj is String) {
print(obj.length)
}
val items = listOf(1, 2, 3, 4)
items.first() == 1
items.last() == 4
items.filter { it % 2 == 0 } // returns [2, 4]
ints.forEach {
if (it == 0) return
print(it)
}
‘’
I call it my billion-dollar mistake. It
was the invention of the null
reference in 1965 ... My goal was to
ensure that all use of references
should be absolutely safe... This has
led to innumerable errors,
vulnerabilities, and system crashes,
which have probably caused a
billion dollars of pain and damage in
the last forty years.
Tony Hoare
var a: String = "abc"
a = null // compilation error
var b: String? = "abc"
b = null // ok
val l = b?.length ?: -1
data class User(val name: String, val age: Int)
You get for free:
▣ equals()
▣ hashCode()
▣ toString() of the form "User(name=John, age=42)",
▣ copy() function
▣ getters()
// Using R.layout.activity_main from the main source set
import kotlinx.android.synthetic.main.activity_main.*
public class MyActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textView.setText("Hello, world!") // Instead of findView(R.id.textView) as TextView
}
}
There is much more:
▣ kotlinlang.org
▣ blog.jetbrains.com/kotlin
▣ kotlinlang.org/docs/resources.html
▣ Kara- An MVC Framework
▣ Android Kotlin Extensions- A collection of Android Kotlin
extensions
▣ KAndroid- Kotlin library for Android
▣ Anko- Pleasant Android application development
Thanks!
Any questions?
You can find this presentation at: shem8.github.io
smagnezi8@gmail.com
Presentation template by SlidesCarnival | Photographs by Unsplash
Credits
Android Development with Kotlin - Jake Wharton
Droidcon SF - Better Android Development with Kotlin and
Gradle
Special thanks to all the people who made and released these
awesome resources for free:
▣ Presentation template by SlidesCarnival
▣ Photographs by Unsplash