Top Banner
Kotlin is charming; The reasons Java engineers should start Kotlin. By Kiyotaka Soranaka:空中清高
51

Kotlin is charming; The reasons Java engineers should start Kotlin.

Jan 21, 2018

Download

Engineering

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Kotlin is charming; The reasons Java engineers should start Kotlin.

Kotlin is charming;

The reasons Java engineers

should start Kotlin.

By Kiyotaka Soranaka:空中清高

Page 2: Kotlin is charming; The reasons Java engineers should start Kotlin.

About me

Name:空中清高(Kiyotaka Soranaka)Work:株式会社ジャストシステム

Page 3: Kotlin is charming; The reasons Java engineers should start Kotlin.

株式会社ジャストシステム

創業38年、売上182億、平均年収900万円

Javaのプロダクト採用は1.0から。一太郎Ark:1996年開発着手。

JUST.SYSTEMS

Page 4: Kotlin is charming; The reasons Java engineers should start Kotlin.

いろんなwebサービスをJavaで作ってます。

などなど...

株式会社ジャストシステムJUST.SYSTEMS

Page 5: Kotlin is charming; The reasons Java engineers should start Kotlin.

Agenda

・What is Kotlin?・Why Kotlin?・Kotlin VS Java・Kotlin AND Java

Page 6: Kotlin is charming; The reasons Java engineers should start Kotlin.

What is Kotlin?

Page 7: Kotlin is charming; The reasons Java engineers should start Kotlin.

Statically typed programming language

for the JVM, Android and the browser

https://kotlinlang.org/

Page 8: Kotlin is charming; The reasons Java engineers should start Kotlin.

Google is adding Kotlin as an official

programming language for Android

development

https://android-developers.googleblog.com/2017/05/android-announces-support-for-kotlin.html

Page 9: Kotlin is charming; The reasons Java engineers should start Kotlin.

Why Kotlin?

Page 10: Kotlin is charming; The reasons Java engineers should start Kotlin.

ConciseSafeVersatileInteroperableTooling

https://kotlinlang.org/

Page 11: Kotlin is charming; The reasons Java engineers should start Kotlin.

ConciseSafeVersatileInteroperableTooling

Page 12: Kotlin is charming; The reasons Java engineers should start Kotlin.

// Create a POJO in a single line

data class Person(val name: String, val age: Int)

// No need "new" keyword and Semicolons are optional.

val person = Person("Kiyotaka Soranaka", 29)

// String Templates

println("My name is ${person.name}.")

// > My name is Kiyotaka Soranaka.

Concise

Page 13: Kotlin is charming; The reasons Java engineers should start Kotlin.

ConciseSafeVersatileInteroperableTooling

Page 14: Kotlin is charming; The reasons Java engineers should start Kotlin.

// Null Safe: These codes cause error at compile time.

val str: String = null // compile error:String is non-null.

val nullable: String? = null // String? is nullable.

val length:Int = nullable.length // compile error:String? is nullable.

// Below codes don't cause error.

val length:Int = nullable!!.length // But this cause error at runtime if ‘nullable’ is null.

val length:Int = if (nullable != null) { nullable.length } else { 0 } // Smart-cast

val length:Int = nullable?.length ?: 0 // "?." is Safe Calls and "?:" is Elvis Operator.

Safe

Page 15: Kotlin is charming; The reasons Java engineers should start Kotlin.

ConciseSafeVersatileInteroperableTooling

Page 16: Kotlin is charming; The reasons Java engineers should start Kotlin.

Versatile

・Android Development.

・Write code in Kotlin and target JavaScript.

・Application Server.

・Spring Framework support Kotlin.

・Enterprise Java EE.

Page 17: Kotlin is charming; The reasons Java engineers should start Kotlin.

ConciseSafeVersatileInteroperableTooling

Page 18: Kotlin is charming; The reasons Java engineers should start Kotlin.

Interoperable

Kotlin is of 100% Java compatibility.

You can use any existing library on the JVM.

Page 19: Kotlin is charming; The reasons Java engineers should start Kotlin.

ConciseSafeVersatileInteroperableTooling

Page 20: Kotlin is charming; The reasons Java engineers should start Kotlin.

Tooling

IntelliJ IDEA is very powerful!

It is developed by JetBrains, where the team

that created Kotlin itself belongs.

Page 21: Kotlin is charming; The reasons Java engineers should start Kotlin.

Tooling

(Win)Alt + Enter, (Mac) ⌥Enter

Page 22: Kotlin is charming; The reasons Java engineers should start Kotlin.

Tooling

(Win)Ctrl + Alt + Shift + K

(Mac) ⌥⇧⌘K

Convert Java to Kotlin.

Page 23: Kotlin is charming; The reasons Java engineers should start Kotlin.

Kotlin VS Java

Page 24: Kotlin is charming; The reasons Java engineers should start Kotlin.

public final class Person {

public Person(String name, int age) {

this.name = name;

this.age = age;

}

private final String name;

private int age;

public String getName() { return name; }

public int getAge() { return age; }

public void setAge(int age) {

this.age = age;

}

}

Kotlin JavaKotlin VS Java

Page 25: Kotlin is charming; The reasons Java engineers should start Kotlin.

class Person(name: String, age: Int) {

val name: String = name

var age: Int = age

}

'val' and 'var' mean read-only and

mutable, respectively.

getter/setter are automatically

generated.

public final class Person {

public Person(String name, int age) {

this.name = name;

this.age = age;

}

private final String name;

private int age;

public String getName() { return name; }

public int getAge() { return age; }

public void setAge(int age) {

this.age = age;

}

}

Kotlin: Level 1 JavaKotlin VS Java

Page 26: Kotlin is charming; The reasons Java engineers should start Kotlin.

Kotlin: Level 2 JavaKotlin VS Java

class Person(val name: String,

var age: Int)

Properties are automatically generated.

public final class Person {

public Person(String name, int age) {

this.name = name;

this.age = age;

}

private final String name;

private int age;

public String getName() { return name; }

public int getAge() { return age; }

public void setAge(int age) {

this.age = age;

}

}

Page 27: Kotlin is charming; The reasons Java engineers should start Kotlin.

Kotlin: Level 3 JavaKotlin VS Java

data class Person(val name: String,

var age: Int)

Several members are automatically

generated; e.g.,

getter/setter, equals(), hashCode(),

toString(), copy(), etc..

val person = Person("Bob", 5)

println("$person")

// > Person(name=Bob, age=5)

https://kotlinlang.org/docs/reference/dat

a-classes.html

public final class Person {

public Person(String name, int age) {

this.name = name;

this.age = age;

}

private final String name;

private int age;

public String getName() { return name; }

public int getAge() { return age; }

public void setAge(int age) {

this.age = age;

}

@Override

public boolean equals(Object obj) {

...

Page 28: Kotlin is charming; The reasons Java engineers should start Kotlin.

Kotlin JavaKotlin VS Java

data class Person(val name: String = "",

var age: Int = 0)

You can use default values.

'Telescoping' is not needed.

'Telescoping' is as follows.

public Person() {

this("");

}

public Person(String name) {

this(name, 0);

}

public Person(String name, int age) {

this.name = name;

this.age = age;

}

Page 29: Kotlin is charming; The reasons Java engineers should start Kotlin.

Kotlin JavaKotlin VS Java

data class Person(var height: Int = 155,

var weight: Int = 50)

By using 'Named Arguments',

you don't have to care the order of the

arguments.

val p1 = Person(height = 170, weight = 60)

val p2= Person(weight = 60, height = 170)

val p3= Person(height = 170)

val p4= Person(weight = 60)

https://kotlinlang.org/docs/reference/fun

ctions.html

public Person() {

this(155);

}

public Person(int height) {

this(height, 50);

}

public Person(int height, int weight) {

this.height = height;// cm

this.weight = weight;// kg

}

Person p1 = new Person(170, 60);

Person p2 = new Person(60, 170);

Page 30: Kotlin is charming; The reasons Java engineers should start Kotlin.

Kotlin: Level 1 JavaKotlin VS Java

val cache = HashMap<String, Calendar>()

fun getJJUGDate(): Date {

var jjugCal = cache["JJUG"]

if (jjugCal == null) {

jjugCal = Calendar.getInstance()

jjugCal.set(2017, Calendar.MAY, 20)

cache.put("JJUG", jjugCal)

}

return jjugCal!!.time

}

Map<String, Calendar> cache = new

HashMap<>();

public Date getJJUGDate() {

Calendar jjugCal = cache.get("JJUG");

if (jjugCal == null) {

jjugCal = Calendar.getInstance();

jjugCal.set(2017, Calendar.MAY, 20);

cache.put("JJUG", jjugCal);

}

return jjugCal.getTime();

}

Page 31: Kotlin is charming; The reasons Java engineers should start Kotlin.

Kotlin: Level 1 JavaKotlin VS Java

val cache = HashMap<String, Calendar>()

fun getJJUGDate(): Date {

var jjugCal = cache["JJUG"]

if (jjugCal == null) {

jjugCal = Calendar.getInstance()

jjugCal.set(2017, Calendar.MAY, 20)

cache.put("JJUG", jjugCal)

}

return jjugCal!!.time

}

Map<String, Calendar> cache = new

HashMap<>();

public Date getJJUGDate() {

Calendar jjugCal = cache.get("JJUG");

if (jjugCal == null) {

jjugCal = Calendar.getInstance();

jjugCal.set(2017, Calendar.MAY, 20);

cache.put("JJUG", jjugCal);

}

return jjugCal.getTime();

}

This code almost the same as Java.

But you can modify more concise.

Page 32: Kotlin is charming; The reasons Java engineers should start Kotlin.

Kotlin: Level 1 JavaKotlin VS Java

val cache = HashMap<String, Calendar>()

fun getJJUGDate(): Date {

var jjugCal = cache["JJUG"]

if (jjugCal == null) {

jjugCal = Calendar.getInstance()

jjugCal.set(2017, Calendar.MAY, 20)

cache.put("JJUG", jjugCal)

}

return jjugCal!!.time

}

Map<String, Calendar> cache = new

HashMap<>();

public Date getJJUGDate() {

Calendar jjugCal = cache.get("JJUG");

if (jjugCal == null) {

jjugCal = Calendar.getInstance();

jjugCal.set(2017, Calendar.MAY, 20);

cache.put("JJUG", jjugCal);

}

return jjugCal.getTime();

}

To be more concise.

・Simplify the code to initialize the calendar.

・Change the type of "jjugCal" to read-only Calendar.

Page 33: Kotlin is charming; The reasons Java engineers should start Kotlin.

Kotlin: Level 2 JavaKotlin VS Java

val cache = HashMap<String, Calendar>()

fun getJJUGDate(): Date {

val jjugCal = cache["JJUG"] ?:

Calendar.getInstance().apply {

set(2017, Calendar.MAY, 20)

cache.put("JJUG", this)

}

return jjugCal.time

}

Map<String, Calendar> cache = new

HashMap<>();

public Date getJJUGDate() {

Calendar jjugCal = cache.get("JJUG");

if (jjugCal == null) {

jjugCal = Calendar.getInstance();

jjugCal.set(2017, Calendar.MAY, 20);

cache.put("JJUG", jjugCal);

}

return jjugCal.getTime();

}

Page 34: Kotlin is charming; The reasons Java engineers should start Kotlin.

Kotlin: Level 2 JavaKotlin VS Java

val cache = HashMap<String, Calendar>()

fun getJJUGDate(): Date {

val jjugCal = cache["JJUG"] ?:

Calendar.getInstance().apply {

set(2017, Calendar.MAY, 20)

cache.put("JJUG", this)

}

return jjugCal.time

}

Map<String, Calendar> cache = new

HashMap<>();

public Date getJJUGDate() {

Calendar jjugCal = cache.get("JJUG");

if (jjugCal == null) {

jjugCal = Calendar.getInstance();

jjugCal.set(2017, Calendar.MAY, 20);

cache.put("JJUG", jjugCal);

}

return jjugCal.getTime();

}

Use "?:"(Elvis Operator).

Use "apply"(scoping function).

Type of "jjugCal" is read-only Calendar.

Page 35: Kotlin is charming; The reasons Java engineers should start Kotlin.

Kotlin: Level 2 JavaKotlin VS Java

val cache = HashMap<String, Calendar>()

fun getJJUGDate(): Date {

val jjugCal = cache["JJUG"] ?:

Calendar.getInstance().apply {

set(2017, Calendar.MAY, 20)

cache.put("JJUG", this)

}

return jjugCal.time

}

Map<String, Calendar> cache = new

HashMap<>();

public Date getJJUGDate() {

Calendar jjugCal = cache.get("JJUG");

if (jjugCal == null) {

jjugCal = Calendar.getInstance();

jjugCal.set(2017, Calendar.MAY, 20);

cache.put("JJUG", jjugCal);

}

return jjugCal.getTime();

}

To be more concise.

・Delete local variable "jjugCal".

・Use kotlin stdlib function.

Page 36: Kotlin is charming; The reasons Java engineers should start Kotlin.

Kotlin: Level 3 JavaKotlin VS Java

val cache = HashMap<String, Calendar>()

fun getJJUGDate(): Date {

return cache.getOrPut("JJUG", {

Calendar.getInstance().apply {

set(2017, Calendar.MAY, 20)

}

}).time

}

Map<String, Calendar> cache = new

HashMap<>();

public Date getJJUGDate() {

Calendar jjugCal = cache.get("JJUG");

if (jjugCal == null) {

jjugCal = Calendar.getInstance();

jjugCal.set(2017, Calendar.MAY, 20);

cache.put("JJUG", jjugCal);

}

return jjugCal.getTime();

}

Page 37: Kotlin is charming; The reasons Java engineers should start Kotlin.

Kotlin: Level 3 JavaKotlin VS Java

val cache = HashMap<String, Calendar>()

fun getJJUGDate(): Date {

return cache.getOrPut("JJUG", {

Calendar.getInstance().apply {

set(2017, Calendar.MAY, 20)

}

}).time

}

Map<String, Calendar> cache = new

HashMap<>();

public Date getJJUGDate() {

Calendar jjugCal = cache.get("JJUG");

if (jjugCal == null) {

jjugCal = Calendar.getInstance();

jjugCal.set(2017, Calendar.MAY, 20);

cache.put("JJUG", jjugCal);

}

return jjugCal.getTime();

}

Use kotlin.stdlib function.

Delete the local variable "jjugCal".

Page 38: Kotlin is charming; The reasons Java engineers should start Kotlin.

Kotlin: Level 3 JavaKotlin VS Java

val cache = HashMap<String, Calendar>()

fun getJJUGDate(): Date {

return cache.getOrPut("JJUG", {

Calendar.getInstance().apply {

set(2017, Calendar.MAY, 20)

}

}).time

}

Map<String, Calendar> cache = new

HashMap<>();

public Date getJJUGDate() {

Calendar jjugCal = cache.get("JJUG");

if (jjugCal == null) {

jjugCal = Calendar.getInstance();

jjugCal.set(2017, Calendar.MAY, 20);

cache.put("JJUG", jjugCal);

}

return jjugCal.getTime();

}

To be more concise.

・"return" and return type is optional.

Page 39: Kotlin is charming; The reasons Java engineers should start Kotlin.

Kotlin: Level 4 JavaKotlin VS Java

val cache = HashMap<String, Calendar>()

fun getJJUGDate() =

cache .getOrPut("JJUG", {

Calendar.getInstance().apply {

set(2017, Calendar.MAY, 20)

}

}).time

Map<String, Calendar> cache = new

HashMap<>();

public Date getJJUGDate() {

Calendar jjugCal = cache.get("JJUG");

if (jjugCal == null) {

jjugCal = Calendar.getInstance();

jjugCal.set(2017, Calendar.MAY, 20);

cache.put("JJUG", jjugCal);

}

return jjugCal.getTime();

}

Page 40: Kotlin is charming; The reasons Java engineers should start Kotlin.

Kotlin: Level 4 JavaKotlin VS Java

val cache = HashMap<String, Calendar>()

fun getJJUGDate() =

cache .getOrPut("JJUG", {

Calendar.getInstance().apply {

set(2017, Calendar.MAY, 20)

}

}).time

Map<String, Calendar> cache = new

HashMap<>();

public Date getJJUGDate() {

Calendar jjugCal = cache.get("JJUG");

if (jjugCal == null) {

jjugCal = Calendar.getInstance();

jjugCal.set(2017, Calendar.MAY, 20);

cache.put("JJUG", jjugCal);

}

return jjugCal.getTime();

}

"return" is omitted.

Page 41: Kotlin is charming; The reasons Java engineers should start Kotlin.

Kotlin AND Java

Page 42: Kotlin is charming; The reasons Java engineers should start Kotlin.

Kotlin JavaKotlin AND Java

package example.kotlin

Import example.java.Person

fun example() {

val p = Person("Bob", 5)

p.age = p.age + 1

}

package example.java;

public class Person {

public Person(String name, int age) {

this.name = name;

this.age = age;

}

private final String name;

private int age;

public String getName() { return name; }

public int getAge() { return age; }

public void setAge(int age) {

this.age = age;

}

}

Page 43: Kotlin is charming; The reasons Java engineers should start Kotlin.

Kotlin JavaKotlin AND Java

You can use any existing library on JVM.

Jackson, for example,

data class Data(val id:Int = 0,

val name:String = "")

val mapper = ObjectMapper()

val data = Data(10, "Soranaka")

val json = mapper

.writeValueAsString(data)

println(json)

// > {"id":10,"name":"Soranaka"}

val data2 = mapper

.readValue(json, Data::class.java)

println(data2)

// > Data(id=10, name=Soranaka)

Page 44: Kotlin is charming; The reasons Java engineers should start Kotlin.

Kotlin JavaKotlin AND Java

data class Data(val id:Int,var name:String) Data data = new Data(10, "J");

data.setName(data.getName() + "JUG");

System.out.println(data);

// > Data(id=10, name=JJUG)

Page 45: Kotlin is charming; The reasons Java engineers should start Kotlin.

Kotlin JavaKotlin AND Java

class KotlinUtil {

val cache =

HashMap<String, Calendar>()

fun getJJUGDate() =

cache .getOrPut("JJUG", {

Calendar.getInstance().apply {

set(2017, Calendar.MAY, 20)

}

}).time

}

KotlinUtil kotlinUtil = new KotlinUtil();

Date jjugDate = kotlinUtil.getJJUGDate();

Page 46: Kotlin is charming; The reasons Java engineers should start Kotlin.

Kotlin JavaKotlin AND Java

class KotlinUtil {

val cache =

HashMap<String, Calendar>()

fun getJJUGDate() =

cache .getOrPut("JJUG", {

Calendar.getInstance().apply {

set(2017, Calendar.MAY, 20)

}

}).time

}

KotlinUtil kotlinUtil = new KotlinUtil();

Date jjugDate = kotlinUtil.getJJUGDate();

Do you want it to be singleton?

Page 47: Kotlin is charming; The reasons Java engineers should start Kotlin.

Kotlin JavaKotlin AND Java

object KotlinUtil {

val cache =

HashMap<String, Calendar>()

fun getJJUGDate() =

cache .getOrPut("JJUG", {

Calendar.getInstance().apply {

set(2017, Calendar.MAY, 20)

}

}).time

}

Date jjugDate =

KotlinUtil.INSTANCE.getJJUGDate();

Page 48: Kotlin is charming; The reasons Java engineers should start Kotlin.

Kotlin JavaKotlin AND Java

object KotlinUtil {

val cache =

HashMap<String, Calendar>()

fun getJJUGDate() =

cache .getOrPut("JJUG", {

Calendar.getInstance().apply {

set(2017, Calendar.MAY, 20)

}

}).time

}

Date jjugDate =

KotlinUtil.INSTANCE.getJJUGDate();

Do you want it to be static?

Page 49: Kotlin is charming; The reasons Java engineers should start Kotlin.

Kotlin JavaKotlin AND Java

class KotlinUtil {

companion object {

private val cache =

HashMap<String, Calendar>()

@JvmStatic

fun getJJUGDate() =

cache.getOrPut("JJUG", {

Calendar.getInstance().apply {

set(2017, Calendar.MAY, 20)

}

}).time

}

}

Date jjugDate = KotlinUtil.getJJUGDate();

Page 50: Kotlin is charming; The reasons Java engineers should start Kotlin.

株式会社ジャストシステム

Kotlinを使える仕事に興味を持った方ぜひ、一緒に働きましょう!

JUST.SYSTEMS

Page 51: Kotlin is charming; The reasons Java engineers should start Kotlin.

Thank you

Kiyotaka Soranaka:空中清高@soranakk