Se ha denunciado esta presentación.

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

3

Compartir

Cargando en…3
×
1 de 51
1 de 51

Más Contenido Relacionado

A los espectadores también les gustó

Más de JustSystems Corporation

Audiolibros relacionados

Gratis con una prueba de 30 días de Scribd

Ver todo

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

Notas del editor

  • 簡潔な記述が可能
  • POJOクラスはKotlinではワンライナーで書けます。
    “data”というキーワードを付けたクラスはgetter/setterやequals(),hashCode(),toString(),copy()といったメソッドが自動的に生成されます。
    オブジェクトを生成するとき、”new”キーワードは必要ありません。
    また「;」は省略可能です。(普通、省略します。)
    さらに、文字列テンプレートが言語機能として実装されていて、文字列中の$マークの直後はプログラムの変数を直接記載できます。これはすごく読みやすいです。
  • Kotlinの大きな特徴の一つはNull Safeな言語であることです。
    普通の型はnon-nullな型なのでnullを代入することができません。
    代入しようとするとコンパイルエラーになります。
    Nullが代入可能な型は、型名のあとに「?」を付けることで宣言できます。
    ただし、nullableな型の変数にアクセスするときはnullチェックが必須で、これを怠るとコンパイルエラーが発生します。

    Nullableな変数にアクセスする方法は以下のようなものがあります。
    「!!」という、いかにも注意を引きそうな書き方は、「これはnullではない、ワシが保証する」という意味です。
    !!を書いたプログラマが「絶対いけるから、nullじゃないから!!」ってことをコンパイラーに伝える仕組みです。
    コンパイラーは素直なのでこれを受け入れてしまいますが、このケースでは見事に実行時エラーが、悪名高いNullPointerExceptionが発生します。
    つまり、普通のプログラマーならめったに「!!」は使わないので、これが乱用されているコードを見かけたらお察しです。
    nullableな変数は一度nullチェックを行えば、以降のコードではSmart-castによって、自動的に?無しの型に変換してくれます。
    また、「?.」でアクセスすれば、nullじゃないときに続きを実行、nullのときはnullを返す、という動きになります。
    この例では、続く「?:」という演算子を使っています。
    これは「?:」の前がnullのときは「?:」の後を返す、という演算子です。
    なので、if文でチェックしたコードと?.でアクセスして、?:を使ってデフォルト値を設定しているコードは等価です。
  • Kotlinはさまざまな用途で利用できます。
    Androidはもちろんですが、JavaScriptやサーバーアプリケーションなどもKotlinで書くことが可能です。
    後述しますが、Javeのプロジェクトと共存可能なので、既存のJavaプロジェクトに組み込むことも容易です。
    また、Spring FrameworkもKotlinを公式サポートしています。(Spring Framework 5.0+)
  • KotlinはJavaと共存可能です。
    なぜか?
    KotlinをJVMで使う場合、KotlinのコードはJavaの.class形式にコンパイルされます。
    つまり、JVMで使う場合はKotlinはJavaです。
  • ×