# 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)
---

---
### 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
---

---

---

---
### 所需工具、環境、時間
* 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 詠唱師

---

---
## IntelliJ do it for you


---
## Clipboard Java to Kotlin

---

---

---
```kotlin=
return if (result.isNotEmpty()) {
result[0]
} else null
// 該瘦身了
return result.firstOrNull()
```
---
### Java 互操作

---
### Java Getter & Setter as Property

---
### Null Safety

---
### Null safety = Lower Cost

---
```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

---
- Kotlin provide Data Class in Java 8
- Java provide Record in Java 14
---

---
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/

---
#### 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("&", "&")
.replace("<", "<")
.replace(">", ">")
}
```
---
https://engineering.fb.com/2022/10/24/android/android-java-kotlin-migration/

---
Code for Biz Logical
---
### Beyond Java
* Coroutines - Async Programming
* Functional Programming
---
[林信良 - 影響我的重大語言](https://www.ithome.com.tw/voice/154242)

---
### Start from Small
https://tech.trello.com/kotlin-at-trello/

---
```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("{}") }
}
```
---

---
### 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}]"}