SlideShare uma empresa Scribd logo
1 de 25
Baixar para ler offline
Kotlin
No excuses, switch to Kotlin
Thijs Suijten
Mobile developer
Q42
@tsuijten
Getting everybody
onboard
Getting everybody
onboard
The team
Getting everybody
onboard
Q42
Getting everybody
onboard
PostNL (product owner)
fun reformat(str: String,
normalize: Boolean = true,
upperCase: Boolean = true): String {

...

}
Functions
reformat("Hello world")

reformat("Hello world", false, false)

reformat("Hello world", upperCase = false)
// Single-Expression functions
fun twiceTheFun(x: Int) = x * 2
class Person(val firstName: String, 

val age: Int, 

val locale: Locale = ENGLISH) {

fun sayHello() = println("Hi $firstName!")

}
Classes
val thijs = Person("Thijs", 34)

thijs.sayHello() // Hi Thijs!
class Cat(name: String) {

val name: String = name

}
val x: String? = getSomeOptionalString()

x.length // Does not compile.
Null safety / optionals
val y: String = null // Does not compile.
if (x != null) {

x.length // Compiles!

}
// Safe call

val optionalLength = x?.length



// Elvis operator.

val length = x?.length ?: -1
Data classes
class User(val name: String, val age: Int)
val thijs = User("Thijs", 35)
println(thijs) // User(name=Thijs, age=35)
val bday = thijs.copy(age = 36) // Named params!

println(bday) // User(name=Thijs, age=36)
data
Data classes
public class User {

private String name;

private int age;



public String getName() {

return name;

}



public void setName(String name) {

this.name = name;

}



public int getAge() {

return age;

}



public void setAge(int age) {

this.age = age;

}



@Override

public boolean equals(Object o) {

if (this == o) return true;

if (o == null || getClass() != o.getClass()) return false;



User user = (User) o;



if (age != user.age) return false;

return name != null ? name.equals(user.name) : user.name == null;



}



@Override

public int hashCode() {

int result = name != null ? name.hashCode() : 0;

result = 31 * result + age;

return result;

}



@Override

public String toString() {

return "User{" +

"name='" + name + ''' +

", age=" + age +

'}';

}

}
Functional / lambas
val sum = listOf(1, 2, 3, 4, 5)

.filter { number -> number < 5 }

.map { it * 2 }

.sum()



println(sum) // 20
listOf("Zack", "Bob", "Thijs", "Julie", "Rob")

.filter { it.length > 3 }

.sorted()

.forEach { println(it) } // Julie Thijs Zack
When statement
// Java

if (firstName.equals("Dan")) {

person.setTeam(programmers);

} else if (lastName.equals("Jamie")) {

person.setTeam(designers);

} else {

person.setTeam(others);

}
// Kotlin

when {

firstName == “Dan" -> person.team = programmers

lastName == "Jamie" -> person.team = designers

else -> person.team = others

}
When statement
// Java

switch (firstName) {

case "Dan":

case "Jay":

person.setTeam(programmers);

break;

case "Jamie":

person.setTeam(designers);

break;

default:

person.setTeam(others);

}
// Kotlin

when (firstName) {

"Dan", "Jay" -> person.team = programmers

"Jamie" -> person.team = designers

else -> person.team = others

}
When statement
when (x) {

1 -> print("x = 1")

is Int -> print(x + 1)

is String -> print(x.length + 1)

is IntArray -> print(x.sum())

in 10..20 -> print("Between 10 and 20")

}
val outcome = when(x) {

1 -> "x = 1"

"Hello" -> "is it me you're looking for?"

in 10..20 -> "Between 10 and 20"

}
Extensions
// Extension function
fun String.last(): Char {

return this[lastIndex]

}
println("Hello!".last())
// Extension property
val String.first: Char get() = this[0]


println("Hello! ".first) // H
Kotlin Android extensions
<TextView android:id="@+id/greeting"

android:layout_width="match_parent"

android:layout_height="wrap_content"/>



<Button android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Click me!"/>
override fun onCreate(savedInstanceState: Bundle?) {

//…


greeting.text = "Hello"

button.onClick { toast("Button clicked!") }

}
Kotlin
..
thijs@q42.nl / @tsuijten

Mais conteúdo relacionado

Mais procurados

Large Scale JavaScript with TypeScript
Large Scale JavaScript with TypeScriptLarge Scale JavaScript with TypeScript
Large Scale JavaScript with TypeScript
Oliver Zeigermann
 
Kotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developersKotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developers
Bartosz Kosarzycki
 
Swift and Kotlin Presentation
Swift and Kotlin PresentationSwift and Kotlin Presentation
Swift and Kotlin Presentation
Andrzej Sitek
 
The Kotlin Programming Language
The Kotlin Programming LanguageThe Kotlin Programming Language
The Kotlin Programming Language
intelliyole
 

Mais procurados (20)

Little Helpers for Android Development with Kotlin
Little Helpers for Android Development with KotlinLittle Helpers for Android Development with Kotlin
Little Helpers for Android Development with Kotlin
 
Exploring Anko Components, Kotlin, Android
Exploring Anko Components, Kotlin, AndroidExploring Anko Components, Kotlin, Android
Exploring Anko Components, Kotlin, Android
 
API management with Taffy and API Blueprint
API management with Taffy and API BlueprintAPI management with Taffy and API Blueprint
API management with Taffy and API Blueprint
 
Intro to Kotlin
Intro to KotlinIntro to Kotlin
Intro to Kotlin
 
Introduction to kotlin
Introduction to kotlinIntroduction to kotlin
Introduction to kotlin
 
Fall in love with Kotlin
Fall in love with KotlinFall in love with Kotlin
Fall in love with Kotlin
 
Large Scale JavaScript with TypeScript
Large Scale JavaScript with TypeScriptLarge Scale JavaScript with TypeScript
Large Scale JavaScript with TypeScript
 
Kotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developersKotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developers
 
Swift and Kotlin Presentation
Swift and Kotlin PresentationSwift and Kotlin Presentation
Swift and Kotlin Presentation
 
Building microservices with Kotlin
Building microservices with KotlinBuilding microservices with Kotlin
Building microservices with Kotlin
 
Kotlin - Better Java
Kotlin - Better JavaKotlin - Better Java
Kotlin - Better Java
 
Introduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demoIntroduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demo
 
eMan Dev Meetup: Kotlin - A Language we should know it exists (part 02/03) 18...
eMan Dev Meetup: Kotlin - A Language we should know it exists (part 02/03) 18...eMan Dev Meetup: Kotlin - A Language we should know it exists (part 02/03) 18...
eMan Dev Meetup: Kotlin - A Language we should know it exists (part 02/03) 18...
 
Kotlin in action
Kotlin in actionKotlin in action
Kotlin in action
 
The Kotlin Programming Language
The Kotlin Programming LanguageThe Kotlin Programming Language
The Kotlin Programming Language
 
Rust Workshop - NITC FOSSMEET 2017
Rust Workshop - NITC FOSSMEET 2017 Rust Workshop - NITC FOSSMEET 2017
Rust Workshop - NITC FOSSMEET 2017
 
A quick and fast intro to Kotlin
A quick and fast intro to Kotlin A quick and fast intro to Kotlin
A quick and fast intro to Kotlin
 
Groovy Programming Language
Groovy Programming LanguageGroovy Programming Language
Groovy Programming Language
 
Groovy presentation
Groovy presentationGroovy presentation
Groovy presentation
 
Kotlin Overview
Kotlin OverviewKotlin Overview
Kotlin Overview
 

Destaque

Smoothing Your Java with DSLs
Smoothing Your Java with DSLsSmoothing Your Java with DSLs
Smoothing Your Java with DSLs
intelliyole
 
[JVMLS 12] Kotlin / Java Interop
[JVMLS 12] Kotlin / Java Interop[JVMLS 12] Kotlin / Java Interop
[JVMLS 12] Kotlin / Java Interop
Andrey Breslav
 

Destaque (11)

Android Workshop
Android WorkshopAndroid Workshop
Android Workshop
 
A brief introduction to Realm with Kotlin
A brief introduction to Realm with KotlinA brief introduction to Realm with Kotlin
A brief introduction to Realm with Kotlin
 
Android & Kotlin - The code awakens #03
Android & Kotlin - The code awakens #03Android & Kotlin - The code awakens #03
Android & Kotlin - The code awakens #03
 
Android & Kotlin - The code awakens #01
Android & Kotlin - The code awakens #01Android & Kotlin - The code awakens #01
Android & Kotlin - The code awakens #01
 
Smoothing Your Java with DSLs
Smoothing Your Java with DSLsSmoothing Your Java with DSLs
Smoothing Your Java with DSLs
 
Kotlin Advanced - language reference for Android developers
Kotlin Advanced - language reference for Android developers Kotlin Advanced - language reference for Android developers
Kotlin Advanced - language reference for Android developers
 
Getting started-kotlin-android
Getting started-kotlin-androidGetting started-kotlin-android
Getting started-kotlin-android
 
[JVMLS 12] Kotlin / Java Interop
[JVMLS 12] Kotlin / Java Interop[JVMLS 12] Kotlin / Java Interop
[JVMLS 12] Kotlin / Java Interop
 
No excuses, switch to kotlin
No excuses, switch to kotlinNo excuses, switch to kotlin
No excuses, switch to kotlin
 
First few months with Kotlin - Introduction through android examples
First few months with Kotlin - Introduction through android examplesFirst few months with Kotlin - Introduction through android examples
First few months with Kotlin - Introduction through android examples
 
Светлана Исакова «Язык Kotlin»
Светлана Исакова «Язык Kotlin»Светлана Исакова «Язык Kotlin»
Светлана Исакова «Язык Kotlin»
 

Semelhante a No excuses, switch to kotlin

About java
About javaAbout java
About java
Jay Xu
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
Loïc Descotte
 

Semelhante a No excuses, switch to kotlin (20)

Introduction kot iin
Introduction kot iinIntroduction kot iin
Introduction kot iin
 
Kotlin
KotlinKotlin
Kotlin
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
 
Meetup di GDG Italia - Leonardo Pirro - Codemotion Rome 2018
Meetup di GDG Italia - Leonardo Pirro -  Codemotion Rome 2018 Meetup di GDG Italia - Leonardo Pirro -  Codemotion Rome 2018
Meetup di GDG Italia - Leonardo Pirro - Codemotion Rome 2018
 
Kotlin Overview (PT-BR)
Kotlin Overview (PT-BR)Kotlin Overview (PT-BR)
Kotlin Overview (PT-BR)
 
Swift Study #7
Swift Study #7Swift Study #7
Swift Study #7
 
About java
About javaAbout java
About java
 
Kotlin : Happy Development
Kotlin : Happy DevelopmentKotlin : Happy Development
Kotlin : Happy Development
 
かとうの Kotlin 講座 こってり版
かとうの Kotlin 講座 こってり版かとうの Kotlin 講座 こってり版
かとうの Kotlin 講座 こってり版
 
From java to kotlin beyond alt+shift+cmd+k
From java to kotlin beyond alt+shift+cmd+kFrom java to kotlin beyond alt+shift+cmd+k
From java to kotlin beyond alt+shift+cmd+k
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Hello kotlin | An Event by DSC Unideb
Hello kotlin | An Event by DSC UnidebHello kotlin | An Event by DSC Unideb
Hello kotlin | An Event by DSC Unideb
 
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
 
Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)
 
Introduzione a C#
Introduzione a C#Introduzione a C#
Introduzione a C#
 
여자개발자모임터 6주년 개발 세미나 - Scala Language
여자개발자모임터 6주년 개발 세미나 - Scala Language여자개발자모임터 6주년 개발 세미나 - Scala Language
여자개발자모임터 6주년 개발 세미나 - Scala Language
 
What's in Kotlin for us - Alexandre Greschon, MyHeritage
What's in Kotlin for us - Alexandre Greschon, MyHeritageWhat's in Kotlin for us - Alexandre Greschon, MyHeritage
What's in Kotlin for us - Alexandre Greschon, MyHeritage
 
Why Kotlin makes Java null and void
Why Kotlin makes Java null and voidWhy Kotlin makes Java null and void
Why Kotlin makes Java null and void
 
Java VS Python
Java VS PythonJava VS Python
Java VS Python
 

Último

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Último (20)

A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 

No excuses, switch to kotlin

  • 3.
  • 4.
  • 5.
  • 6.
  • 11.
  • 12. fun reformat(str: String, normalize: Boolean = true, upperCase: Boolean = true): String {
 ...
 } Functions reformat("Hello world")
 reformat("Hello world", false, false)
 reformat("Hello world", upperCase = false) // Single-Expression functions fun twiceTheFun(x: Int) = x * 2
  • 13. class Person(val firstName: String, 
 val age: Int, 
 val locale: Locale = ENGLISH) {
 fun sayHello() = println("Hi $firstName!")
 } Classes val thijs = Person("Thijs", 34)
 thijs.sayHello() // Hi Thijs! class Cat(name: String) {
 val name: String = name
 }
  • 14. val x: String? = getSomeOptionalString()
 x.length // Does not compile. Null safety / optionals val y: String = null // Does not compile. if (x != null) {
 x.length // Compiles!
 } // Safe call
 val optionalLength = x?.length
 
 // Elvis operator.
 val length = x?.length ?: -1
  • 15. Data classes class User(val name: String, val age: Int) val thijs = User("Thijs", 35) println(thijs) // User(name=Thijs, age=35) val bday = thijs.copy(age = 36) // Named params!
 println(bday) // User(name=Thijs, age=36) data
  • 16. Data classes public class User {
 private String name;
 private int age;
 
 public String getName() {
 return name;
 }
 
 public void setName(String name) {
 this.name = name;
 }
 
 public int getAge() {
 return age;
 }
 
 public void setAge(int age) {
 this.age = age;
 }
 
 @Override
 public boolean equals(Object o) {
 if (this == o) return true;
 if (o == null || getClass() != o.getClass()) return false;
 
 User user = (User) o;
 
 if (age != user.age) return false;
 return name != null ? name.equals(user.name) : user.name == null;
 
 }
 
 @Override
 public int hashCode() {
 int result = name != null ? name.hashCode() : 0;
 result = 31 * result + age;
 return result;
 }
 
 @Override
 public String toString() {
 return "User{" +
 "name='" + name + ''' +
 ", age=" + age +
 '}';
 }
 }
  • 17. Functional / lambas val sum = listOf(1, 2, 3, 4, 5)
 .filter { number -> number < 5 }
 .map { it * 2 }
 .sum()
 
 println(sum) // 20 listOf("Zack", "Bob", "Thijs", "Julie", "Rob")
 .filter { it.length > 3 }
 .sorted()
 .forEach { println(it) } // Julie Thijs Zack
  • 18. When statement // Java
 if (firstName.equals("Dan")) {
 person.setTeam(programmers);
 } else if (lastName.equals("Jamie")) {
 person.setTeam(designers);
 } else {
 person.setTeam(others);
 } // Kotlin
 when {
 firstName == “Dan" -> person.team = programmers
 lastName == "Jamie" -> person.team = designers
 else -> person.team = others
 }
  • 19. When statement // Java
 switch (firstName) {
 case "Dan":
 case "Jay":
 person.setTeam(programmers);
 break;
 case "Jamie":
 person.setTeam(designers);
 break;
 default:
 person.setTeam(others);
 } // Kotlin
 when (firstName) {
 "Dan", "Jay" -> person.team = programmers
 "Jamie" -> person.team = designers
 else -> person.team = others
 }
  • 20. When statement when (x) {
 1 -> print("x = 1")
 is Int -> print(x + 1)
 is String -> print(x.length + 1)
 is IntArray -> print(x.sum())
 in 10..20 -> print("Between 10 and 20")
 } val outcome = when(x) {
 1 -> "x = 1"
 "Hello" -> "is it me you're looking for?"
 in 10..20 -> "Between 10 and 20"
 }
  • 21. Extensions // Extension function fun String.last(): Char {
 return this[lastIndex]
 } println("Hello!".last()) // Extension property val String.first: Char get() = this[0] 
 println("Hello! ".first) // H
  • 22. Kotlin Android extensions <TextView android:id="@+id/greeting"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"/>
 
 <Button android:id="@+id/button"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Click me!"/> override fun onCreate(savedInstanceState: Bundle?) {
 //… 
 greeting.text = "Hello"
 button.onClick { toast("Button clicked!") }
 }
  • 24.