設為首頁收藏本站

艾歐踢論壇

 找回密碼
 立即註冊

QQ登錄

只需一步,快速開始

搜索
熱搜: 活動 交友 discuz
查看: 517|回復: 0
打印 上一主題 下一主題

Kotlin / Java 的比較

[複製鏈接]
跳轉到指定樓層
樓主
發表於 2019-12-9 06:19:09 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
以下開始記錄 Kotlin 的優點 (與 Java 比較) :
https://www.androidauthority.com/kotlin-vs-java-783187/

1. 比較簡潔(concise)。
    如 UI 的 findViewById,Kotlin 可直接存取 ui compoent)

2. default null safe。
    Kotlin 變數預設是不允許為 null (Java 是),如果要直接設成 null,要使用 "?=" 設給 l-value。

3. Extension functions。
  
4. Coroutines (Kotlin 導入 suspend 概念,此詞意同 non-blocking 機制,相仿 thread sleep )
    Kotlin 支援 async/await (同 C# async/await)

    // non-blocking coroutine
    val job = launch {
        delay(1000L)  // delay 是 suspend 的 sleep
    }

    // blocking coroutine
    runBlocking {
        delay(1000L)
    }

     * life cycle
         GlobalScope.lauch 的 GlobalScope 是指整個 app 存在時,此 coroutine 就會存在。也可自         訂 coroutine 的 life cycle。
     * 在 coroutine block 中呼叫 func 時,要使用 suspend 修飾字放在 func 前,才能被 corouine         使用。
     * coroutine 的 func : join、cancel、cancelAndJoin
     * timeout
         withTimeout(1300L) {    //withTimeoutOrNull() 則是可以回傳 null
             repeat(1000) { i ->
                 println("I'm sleeping $i ...")
                 delay(500L)
             }
         }


5. 支援 default function parameter (Java 不支援,需要以 function overload 達成)
    支援 properties (Java 沒有,C# 有)
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 轉播轉播 分享分享 分享淘帖
回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

小黑屋|Archiver|手機版|艾歐踢創新工坊    

GMT+8, 2024-5-16 20:53 , Processed in 0.301392 second(s), 18 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表