Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

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

7,499 views

Published on

JJUG CCC 2017 Spring #ccc_c5 の『15:45-16:30
Javaエンジニアから見たKotlinの魅力』の資料です。

Published in: Engineering
  • Be the first to comment

  • Be the first to like this

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

  1. 1. Kotlin is charming; The reasons Java engineers should start Kotlin. By Kiyotaka Soranaka:空中清高
  2. 2. About me Name:空中 清高(Kiyotaka Soranaka) Work:株式会社ジャストシステム
  3. 3. 株式会社ジャストシステム 創業38年、売上182億、平均年収900万円 Javaのプロダクト採用は1.0から。 一太郎Ark:1996年開発着手。 JUST. SYSTEMS
  4. 4. いろんなwebサービスをJavaで作ってます。 などなど... 株式会社ジャストシステムJUST. SYSTEMS
  5. 5. Agenda ・What is Kotlin? ・Why Kotlin? ・Kotlin VS Java ・Kotlin AND Java
  6. 6. What is Kotlin?
  7. 7. Statically typed programming language for the JVM, Android and the browser https://kotlinlang.org/
  8. 8. 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
  9. 9. Why Kotlin?
  10. 10. Concise Safe Versatile Interoperable Tooling https://kotlinlang.org/
  11. 11. Concise Safe Versatile Interoperable Tooling
  12. 12. // 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
  13. 13. Concise Safe Versatile Interoperable Tooling
  14. 14. // 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
  15. 15. Concise Safe Versatile Interoperable Tooling
  16. 16. Versatile ・Android Development. ・Write code in Kotlin and target JavaScript. ・Application Server. ・Spring Framework support Kotlin. ・Enterprise Java EE.
  17. 17. Concise Safe Versatile Interoperable Tooling
  18. 18. Interoperable Kotlin is of 100% Java compatibility. You can use any existing library on the JVM.
  19. 19. Concise Safe Versatile Interoperable Tooling
  20. 20. Tooling IntelliJ IDEA is very powerful! It is developed by JetBrains, where the team that created Kotlin itself belongs.
  21. 21. Tooling (Win)Alt + Enter, (Mac) ⌥Enter
  22. 22. Tooling (Win)Ctrl + Alt + Shift + K (Mac) ⌥⇧⌘K Convert Java to Kotlin.
  23. 23. Kotlin VS Java
  24. 24. 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
  25. 25. 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
  26. 26. 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; } }
  27. 27. 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) { ...
  28. 28. 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; }
  29. 29. 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);
  30. 30. 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(); }
  31. 31. 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.
  32. 32. 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.
  33. 33. 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(); }
  34. 34. 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.
  35. 35. 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.
  36. 36. 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(); }
  37. 37. 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".
  38. 38. 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.
  39. 39. 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(); }
  40. 40. 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.
  41. 41. Kotlin AND Java
  42. 42. 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; } }
  43. 43. 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)
  44. 44. 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)
  45. 45. 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();
  46. 46. 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?
  47. 47. 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();
  48. 48. 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?
  49. 49. 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();
  50. 50. 株式会社ジャストシステム Kotlinを使える仕事に興味を持った方 ぜひ、一緒に働きましょう! JUST. SYSTEMS
  51. 51. Thank you Kiyotaka Soranaka:空中清高 @soranakk

×