# Java x Kotlin 混搭蹦出新滋味 --- - [Brandy :arrow_right: Github](https://github.com/hmchangm/getting-start-QK) - [2022 鐵人賽 Quarkus x Kotlin x Reactive 雲原生服務開發](https://ithelp.ithome.com.tw/users/20135701/ironman/5479) --- ![](https://i.imgur.com/NI1eHrM.png) --- ### Kotlin Backend * [Google ](https://www.youtube.com/watch?v=o14wGByBRAQ) * [Meta aka FB](https://engineering.fb.com/2022/10/24/android/android-java-kotlin-migration/) * [電瀨(沒有少女)](https://github.com/aotter/aotter-quarkus-tutorial) * [More...](https://kotlinlang.org/lp/server-side/case-studies/) --- ### Java at Google * **很多 Java 資產** * Java 11 * Non-blocking, Async --- ![](https://i.imgur.com/Nhy40gZ.png) --- ![](https://i.imgur.com/CHYbiWS.png) --- ![](https://i.imgur.com/1EfjHFG.png) --- ### 所需工具、環境、時間 * IntelliJ * JDK v1.8 * Java & Maven * [Kotlin : Spring Boot](https://kotlinlang.org/docs/maven.html) * [Spring Boot : Kotlin](https://spring.io/guides/tutorials/spring-boot-kotlin/) --- ## AI 詠唱師 ![](https://i.imgur.com/VGmCsZi.png) --- ![](https://i.imgur.com/gr1az09.png) --- ## IntelliJ do it for you ![](https://i.imgur.com/Aj1Rtw0.png) ![](https://i.imgur.com/dgjPEJ9.png) --- ## Clipboard Java to Kotlin ![](https://i.imgur.com/w4oWYCC.png) --- ![](https://i.imgur.com/ntiN3yb.png) --- ![](https://i.imgur.com/WrplGM0.png) --- ```kotlin= return if (result.isNotEmpty()) { result[0] } else null // 該瘦身了 return result.firstOrNull() ``` --- ### Java 互操作 ![](https://i.imgur.com/EG9pzsA.png) --- ### Java Getter & Setter as Property ![](https://i.imgur.com/1lXALMK.png) --- ### Null Safety ![](https://i.imgur.com/Dtj2grU.png) --- ### Null safety = Lower Cost ![](https://i.imgur.com/0P3HMJM.png) --- ```kotlin= var fooString: String? = "foo" // it returns fooString's length or null println(fooString?.length) // it returns fooString's value in uppercase or null println(fooString?.uppercase()) fooCity?.mayor?.name?.uppercase() ``` --- ### 簡潔的程式碼 --- Spring + Kotlin = Modern + Reactive + Productive by Josh Long ![](https://i.imgur.com/uVcwWpI.jpg) --- - Kotlin provide Data Class in Java 8 - Java provide Record in Java 14 --- ![](https://i.imgur.com/ixs06rx.png) --- https://www.baeldung.com/kotlin/lists ```kotlin= val countries = listOf("Germany", "Japan", "Brazil") println(countries[1]) val map = mapOf(1 to "Geeks", 2 to "for", 3 to "Geeks") println(map[2]) ``` --- ### Rich Collection Function https://collection.kotlin.tips/ ![](https://i.imgur.com/jU5ei7U.png) --- #### Chunked and flatten https://pl.kotl.in/4JytTyDxh ```kotlin= val list = listOf("🌱", "🚀", "💡", "🐧", "⚙️", "🤖", "📚") list.chunked(3).map{it.reversed()}.flatten() ``` --- #### GroupBy https://pl.kotl.in/eVjLYhMl6 --- ## Rich built-in fun https://pl.kotl.in/iPULUkvm3 ```kotlin= fun main() { val str = "/deep/path/to/abc" println("substringBefore " + str.substringBefore("/")) println("substringAfterLast: ${str.substringAfterLast("/")}") } ``` ### Extension function ```kotlin= fun String.escapeForXml() : String { return this.replace("&", "&amp;") .replace("<", "&lt;") .replace(">", "&gt;") } ``` --- https://engineering.fb.com/2022/10/24/android/android-java-kotlin-migration/ ![](https://i.imgur.com/JgY3bRv.png) --- Code for Biz Logical --- ### Beyond Java * Coroutines - Async Programming * Functional Programming --- [林信良 - 影響我的重大語言](https://www.ithome.com.tw/voice/154242) ![](https://i.imgur.com/ayAQIlU.jpg) --- ### Start from Small https://tech.trello.com/kotlin-at-trello/ ![](https://i.imgur.com/279Wxjm.png) --- ```kotlin= mockMvc.post("/mockmvc/validate") { contentType = MediaType.APPLICATION_JSON content = mapper.writeValueAsString(Request(Name("admin", ""))) accept = MediaType.APPLICATION_JSON }.andExpect { status { isOk } content { contentType(MediaType.APPLICATION_JSON) } content { json("{}") } } ``` --- ![](https://i.imgur.com/XL93Y6L.png) --- ### Awsome Kotlin * [Spring Framework Kotlin Support](https://docs.spring.io/spring-framework/docs/5.0.0.RELEASE/spring-framework-reference/kotlin.html) * Spring Reactive * [MocKK](https://www.baeldung.com/kotlin/mockk) * [Jackson x Kotlin](https://github.com/FasterXML/jackson-module-kotlin) * https://github.com/pinterest/ktlint --- ### Taiwan Kotlin * [TW kotlin Tips](https://tw.kotlin.tips/) * [范聖佑 - JetBrains 技術傳教士,推廣 Kotlin](https://kraftsman.tw/) --- * https://kotlin.link/ * https://hyperskill.org/tracks/18 * [Idiomatic Kotlin](https://www.youtube.com/watch?v=_6B-6WzufY4) ---
{"metaMigratedAt":"2023-06-17T15:05:30.631Z","metaMigratedFrom":"YAML","title":"為 Java 專案施展 Kotlin 魔法,混搭蹦出新滋味","breaks":true,"description":"View the slide with \"Slide Mode\".","contributors":"[{\"id\":\"28aef4d3-277e-46c5-887c-233edfe0da9f\",\"add\":8857,\"del\":4149}]"}
    848 views