SlideShare a Scribd company logo
Fun With
Kotlin
by Egor Andreevici
INTRO
WHAT’S KOTLIN
INTRO
WHY KOTLIN?
INTRO
PRAGMATISM
Clarity Safety Interoperability Tooling
WHAT’S KOTLIN ABOUT?
INTRO
FOCUS ON USE CASES
INTEROPERATE
WHY PRAGMATIC?
INTRO
WHO MAINTAINS KOTLIN?
INTRO
WHO’S USING KOTLIN?
SYNTAX
SYNTAX
fun sum(a: Int, b: Int): Int {
  return a + b
}
KOTLIN SYNTAX
SYNTAX
fun sum(a: Int, b: Int): Int {
  return a + b
}
KOTLIN SYNTAX
SYNTAX
fun sum(a: Int, b: Int): Int {
  return a + b
}
KOTLIN SYNTAX
SYNTAX
fun sum(a: Int, b: Int): Int {
  return a + b
}
KOTLIN SYNTAX
SYNTAX
fun sum(a: Int, b: Int): Int {
  return a + b
}
KOTLIN SYNTAX
SYNTAX
fun sum(a: Int, b: Int): Int {
  return a + b
}
KOTLIN SYNTAX
SYNTAX
fun sum(a: Int, b: Int): Int = a + b
KOTLIN SYNTAX
SYNTAX
fun sum(a: Int, b: Int) = a + b
KOTLIN SYNTAX
CLASSES
CLASSES
Kotlin Java
class Person public class Person {}
CREATING A CLASS
CLASSES
class Person
class Student : Person()
INHERITANCE
CLASSES
class Person
class Student : Person() // won’t compile!
INHERITANCE
CLASSES
class Person
class Student : Person() // won’t compile!
INHERITANCE
CLASSES
open class Person
class Student : Person() // voila!
INHERITANCE
CLASSES
open class Person(val name: String, var age: Int)
// Java
public class Person {
private final String name;
private Integer age;
public Person(String name, Integer age) { // }
public String getName() { // }
public Integer getAge() { // }
public void setAge(Integer age) { // }
}
CONSTRUCTORS
CLASSES
open class Person(val name: String, var age: Int)
Kotlin Java
val name: String final String name
var age: Int Integer age
VAL & VAR
CLASSES
data class Person(val name: String, var age: Int)
‣ equals() & hashCode() $0
‣ toString() $0
‣ componentN() functions $0
‣ copy() function $0
‣ TOTAL FREE!
DATA CLASSES
CLASSES
val name = person.component1()
val age = person.component2()
COMPONENTN() FUNCTIONS
CLASSES
val name = person.component1()
val age = person.component2()
for ((name, age) in people) {
println("$name is $age years old")
}
DESTRUCTURING DECLARATIONS
CLASSES
class Person {
companion object {
val FIELD_NAME = "name"
fun isOlderThan(person: Person, age: Int) =
person.age > age
}
}
println(Person.isOlderThan(person, 30))
STATIC -> OBJECT
CLASSES
public final class StringUtils {
public static String capitalize(String str) {
// TODO: return capitalized string
return str;
}
}
StringUtils.capitalize(“a”);
UTILS IN JAVA
CLASSES
fun String.capitalize(): String {
// TODO: return capitalized string
return this
}
"a".capitalize()
EXTENSIONS IN KOTLIN
CLASSES
fun String.capitalize(): String {
// TODO: return capitalized string
return this
}
"a".capitalize()
import me.egorand.kotlin.extensions.capitalize
EXTENSIONS IN KOTLIN
NULL-SAFETY
NULL-SAFETY
THE BILLION DOLLAR MISTAKE
NULL SAFETY
var a: String = "abc"
a = null // compilation error
var b: String? = "abc"
b = null // OK
val l = a.length
val l = b.length // error: variable 'b' can be null
NULLABLE VS NON-NULL TYPES
NULL SAFETY
val l = if (b != null) b.length else -1
CHECKING FOR NULL: SIMPLE APPROACH
NULL SAFETY
b?.length
bob?.department?.head?.name
CHECKING FOR NULL: SAFE CALLS
NULL SAFETY
CHECKING FOR NULL: ELVIS OPERATOR
NULL SAFETY
val l: Int = if (b != null) b.length else -1
val l = b?.length ?: -1
CHECKING FOR NULL: ELVIS OPERATOR
NULL SAFETY
val l = b!!.length()
CHECKING FOR NULL: LIVING DANGEROUSLY
FUNCTIONS
FUNCTIONS
fun double(x: Int): Int {
return x * 2
}
val result = double(2)
FUNCTIONS IN KOTLIN
FUNCTIONS
fun Int.times(n: Int) = this * n
println(2.times(3))
INFIX NOTATION
FUNCTIONS
infix fun Int.times(n: Int) = this * n
println(2.times(3))
INFIX NOTATION
FUNCTIONS
infix fun Int.times(n: Int) = this * n
println(2 times 3)
INFIX NOTATION
FUNCTIONS
fun printString(str: String,
prefix: String = "",
postfix: String = "") {
println(prefix + str + postfix)
}
printString("hello")
printString("hello", "<")
printString("hello", "<", ">")
DEFAULT ARGUMENTS
FUNCTIONS
fun printString(str: String,
prefix: String = "",
postfix: String = "") {
println(prefix + str + postfix)
}
printString(“hello”,
prefix = “<“,
postfix = “>”)
NAMED ARGUMENTS
FUNCTIONS
fun <T> forEach(list: List<T>, f: (T) -> Unit) {
for (elem in list) {
f(elem)
}
}
val list = listOf("Alice", "Bob", "Claire")
forEach(list, ::println)
forEach(list, { str -> println(str) })
HIGHER-ORDER FUNCTIONS
FUNCTIONS
fun <T> forEach(list: List<T>, f: (T) -> Unit) {
for (elem in list) {
f(elem)
}
}
val list = listOf("Alice", "Bob", "Claire")
forEach(list, ::println)
forEach(list, { println(it) })
HIGHER-ORDER FUNCTIONS
FUNCTIONS
fun <T> forEach(list: List<T>, f: (T) -> Unit) {
for (elem in list) {
f(elem)
}
}
val list = listOf("Alice", "Bob", "Claire")
forEach(list, ::println)
forEach(list) { println(it) }
HIGHER-ORDER FUNCTIONS
SMART CASTS
SMART CASTS
// Java
private static void demo(Object x) {
if (x instanceof String) {
String str = (String) x;
System.out.println(str.length());
}
}
CASTING IN JAVA
SMART CASTS
// Java
private static void demo(Object x) {
if (x instanceof String) {
String str = (String) x;
System.out.println(str.length());
}
}
// Kotlin
fun demo(x: Any) {
if (x is String) {
println(x.length)
}
}
SMART CASTS IN KOTLIN
SMART CASTS
fun demo(x: Any) {
if (x is String) {
println(x.length)
}
}
IF EXPRESSION
SMART CASTS
fun demo(x: Any) {
if (x !is String) {
println("Not a String")
} else {
println(x.length)
}
}
IF-ELSE EXPRESSION
SMART CASTS
fun demo(x: Any) {
if (x !is String) return
println(x.length)
}
RETURN STATEMENT
SMART CASTS
if (x !is String || x.length == 0) return
if (x is String && x.length > 0)
print(x.length)
BOOLEAN EXPRESSIONS
WHAT’S NEXT?
WHAT’S NEXT?
POST-1.0 ROADMAP
▸ New language features
▸ Java 8/9 support
▸ JavaScript support
▸ Android tooling improvements
▸ IDE features
WHAT’S NEXT?
RESOURCES
▸ Kotlin - http://kotlinlang.org/
▸ Koans - http://try.kotlinlang.org/koans
▸ Blog - https://blog.jetbrains.com/kotlin/
▸ Forum - https://devnet.jetbrains.com/community/kotlin
▸ Slack - http://kotlinlang.slack.com/
Thank you!
@EgorAnd
+EgorAndreevich
http://blog.egorand.me/

More Related Content

PDF
Blazing Fast, Pure Effects without Monads — LambdaConf 2018
PDF
Post-Free: Life After Free Monads
PDF
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
PDF
Scalaz 8 vs Akka Actors
PDF
MTL Versus Free
PDF
The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...
PDF
ZIO Schedule: Conquering Flakiness & Recurrence with Pure Functional Programming
PDF
Hammurabi
Blazing Fast, Pure Effects without Monads — LambdaConf 2018
Post-Free: Life After Free Monads
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
Scalaz 8 vs Akka Actors
MTL Versus Free
The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...
ZIO Schedule: Conquering Flakiness & Recurrence with Pure Functional Programming
Hammurabi

What's hot (20)

PDF
The Death of Final Tagless
PPTX
Scala - where objects and functions meet
PDF
Orthogonal Functional Architecture
PDF
One Monad to Rule Them All
PDF
All Aboard The Scala-to-PureScript Express!
PDF
The Design of the Scalaz 8 Effect System
PDF
Halogen: Past, Present, and Future
PDF
The Next Great Functional Programming Language
PDF
Refactoring Functional Type Classes
PDF
Atomically { Delete Your Actors }
PDF
Apache PIG - User Defined Functions
PDF
Kotlin Bytecode Generation and Runtime Performance
PDF
Scalaz 8: A Whole New Game
PDF
Functor, Apply, Applicative And Monad
PDF
Introduction to functional programming using Ocaml
PDF
Why The Free Monad isn't Free
ZIP
Intro to Pig UDF
PPTX
Kotlin as a Better Java
PPTX
Java 7, 8 & 9 - Moving the language forward
PDF
ZIO Queue
The Death of Final Tagless
Scala - where objects and functions meet
Orthogonal Functional Architecture
One Monad to Rule Them All
All Aboard The Scala-to-PureScript Express!
The Design of the Scalaz 8 Effect System
Halogen: Past, Present, and Future
The Next Great Functional Programming Language
Refactoring Functional Type Classes
Atomically { Delete Your Actors }
Apache PIG - User Defined Functions
Kotlin Bytecode Generation and Runtime Performance
Scalaz 8: A Whole New Game
Functor, Apply, Applicative And Monad
Introduction to functional programming using Ocaml
Why The Free Monad isn't Free
Intro to Pig UDF
Kotlin as a Better Java
Java 7, 8 & 9 - Moving the language forward
ZIO Queue
Ad

Viewers also liked (15)

PPTX
Kotlin
PDF
Kotlin Slides from Devoxx 2011
PPTX
Eval4j @ JVMLS 2014
PDF
Kotlin @ StrangeLoop 2011
PDF
Kotlin @ CSClub & Yandex
PPTX
Flexible Types in Kotlin - JVMLS 2015
PDF
Kotlin @ Devoxx 2011
PPTX
Introduction to Kotlin: Brief and clear
PDF
JVMLS 2016. Coroutines in Kotlin
PDF
Светлана Исакова «Язык Kotlin»
PPTX
Java EE 8 Update
PDF
Be More Productive with Kotlin
PDF
Unit Testing in Kotlin
PDF
Who's More Functional: Kotlin, Groovy, Scala, or Java?
PDF
Functional Reactive Programming with Kotlin on Android - Giorgio Natili - Cod...
Kotlin
Kotlin Slides from Devoxx 2011
Eval4j @ JVMLS 2014
Kotlin @ StrangeLoop 2011
Kotlin @ CSClub & Yandex
Flexible Types in Kotlin - JVMLS 2015
Kotlin @ Devoxx 2011
Introduction to Kotlin: Brief and clear
JVMLS 2016. Coroutines in Kotlin
Светлана Исакова «Язык Kotlin»
Java EE 8 Update
Be More Productive with Kotlin
Unit Testing in Kotlin
Who's More Functional: Kotlin, Groovy, Scala, or Java?
Functional Reactive Programming with Kotlin on Android - Giorgio Natili - Cod...
Ad

Similar to Fun with Kotlin (20)

PDF
Exploring Koltin on Android
PDF
Kotlin for Android Developers
PDF
XKE Typeclass
ODP
Effective way to code in Scala
PDF
Hive Functions Cheat Sheet
PPT
Stacks.ppt
PPT
Stacks.ppt
PDF
Kotlin Starter Pack
PDF
Kotlin: A pragmatic language by JetBrains
PPTX
Kotlin collections
PPT
PDF
Java cheatsheet
PPT
The Kotlin Programming Language
PDF
1 kotlin vs. java: some java issues addressed in kotlin
PDF
Swift the implicit parts
PDF
Kotlin Basics - Apalon Kotlin Sprint Part 2
PPT
Paradigmas de Linguagens de Programacao - Aula #4
PDF
3 kotlin vs. java- what kotlin has that java does not
PPTX
Data Type In Python.pptx
Exploring Koltin on Android
Kotlin for Android Developers
XKE Typeclass
Effective way to code in Scala
Hive Functions Cheat Sheet
Stacks.ppt
Stacks.ppt
Kotlin Starter Pack
Kotlin: A pragmatic language by JetBrains
Kotlin collections
Java cheatsheet
The Kotlin Programming Language
1 kotlin vs. java: some java issues addressed in kotlin
Swift the implicit parts
Kotlin Basics - Apalon Kotlin Sprint Part 2
Paradigmas de Linguagens de Programacao - Aula #4
3 kotlin vs. java- what kotlin has that java does not
Data Type In Python.pptx

Recently uploaded (20)

PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Understanding Forklifts - TECH EHS Solution
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
history of c programming in notes for students .pptx
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
How Creative Agencies Leverage Project Management Software.pdf
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
Essential Infomation Tech presentation.pptx
PDF
System and Network Administration Chapter 2
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Digital Strategies for Manufacturing Companies
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
VVF-Customer-Presentation2025-Ver1.9.pptx
Reimagine Home Health with the Power of Agentic AI​
Understanding Forklifts - TECH EHS Solution
How to Migrate SBCGlobal Email to Yahoo Easily
history of c programming in notes for students .pptx
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
How Creative Agencies Leverage Project Management Software.pdf
Operating system designcfffgfgggggggvggggggggg
How to Choose the Right IT Partner for Your Business in Malaysia
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Softaken Excel to vCard Converter Software.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Essential Infomation Tech presentation.pptx
System and Network Administration Chapter 2
Which alternative to Crystal Reports is best for small or large businesses.pdf
CHAPTER 2 - PM Management and IT Context
Upgrade and Innovation Strategies for SAP ERP Customers
Digital Strategies for Manufacturing Companies

Fun with Kotlin