1. 程式人生 > >Kotlin學習之旅(D8)-From Java to Kotlin

Kotlin學習之旅(D8)-From Java to Kotlin

Kotlin學習之旅第八天

今天的主題是:From Java to Kotlin

前言

學習資源

Kotlin官網線上學習課程(需要翻牆):Kotlin Koans

kotlin必備知識:

首先如果是有Java基礎的童鞋,推薦直接從官網文件上手,可以快速學習Kotlin這門語言的特性和用法。

如果是零基礎的童鞋,那麼推薦看視訊+看書,一般都是直接在youtube上搜索kotlin就可以找到很有有用的視訊了,書籍的話推薦《Kotlin in Action》

這兩天的任務比較輕鬆,因為From Java to Kotlin 其實在前面我們講語法和習慣用法的時候,基本上都已經見過了,這裡其實只是相當於再複習一遍,而且上面的

From Java To Kotlin已經很全了,我就把自己不太熟練的摘錄下來。

常見區別

Null

Java

if(text != null){
  int length = text.length();
}

Kotlin

val length = text?.length

val length = text!!.length // NullPointerException if text == null

?:可以為空,但是如果沒做非空判斷,在編譯的時候會報錯

!!.:可以為空,編譯的時候也不會報錯,但是執行的時候有可能會NPE

Strings II

Java

String text = "First Line\n" +
              "Second Line\n" +
              "Third Line";

Kotlin

val text = """
        |First Line
        |Second Line
        |Third Line
""".trimMargin()

Kotlin裡面通過 ''' 來表示多行字串,通過 | 來表示換行

Bits Operations

Java

final int andResult  = a & b;
final int orResult   = a | b;
final int xorResult  = a ^ b;
final int rightShift = a >> 2;
final int leftShift  = a << 2;

Kotlin

val andResult  = a and b
val orResult   = a or b
val xorResult  = a xor b
val rightShift = a shr 2
val leftShift  = a shl 2

Java 和 Kotlin 的位操作符

Collections

Java

for (int number : numbers) {
  System.out.println(number);
}

for (int number : numbers) {
  if(number > 5) {
    System.out.println(number);
  }
}

Kotlin

numbers.forEach {
    println(it)
}

numbers.filter  { it > 5 }
       .forEach { println(it) }

Kotlin通過 filter{x > 5} 來表示 Java裡面的 if(x > 5) 這個條件語句的區別需要注意

Constructors

Java

public class Utils {

    private Utils() {
      // This utility class is not publicly instantiable
    }

    public static int getScore(int value) {
        return 2 * value;
    }
}

Kotlin

class Utils private constructor() {
    companion object {
        fun getScore(value: Int): Int {
            return 2 * value
        }
    }
}

// another way
object Utils {
    fun getScore(value: Int): Int {
        return 2 * value
    }
}

Java:通過 靜態方法 來實現工具類

Kotlin:一切皆是物件,通過 companion object / ojbect utils 來實現工具類

Class methods

Java

public class Utils {

    private Utils() {
      // This utility class is not publicly instantiable
    }

    public static int triple(int value) {
        return 3 * value;
    }

}

int result = Utils.triple(3);

Kotlin

fun Int.triple(): Int {
  return this * 3
}

var result = 3.triple()

Java:通過static實現類方法

Kotlin:通過擴充套件函式實現類方法

enum

Java

public enum Direction {
        NORTH(1),
        SOUTH(2),
        WEST(3),
        EAST(4);

        int direction;

        Direction(int direction) {
            this.direction = direction;
        }

        public int getDirection() {
            return direction;
        }
    }

Kotlin

enum class Direction constructor(direction: Int) {
    NORTH(1),
    SOUTH(2),
    WEST(3),
    EAST(4);

    var direction: Int = 0
        private set

    init {
        this.direction = direction
    }
}

總結

一開始以為From Java to Kotlin 要講兩天的,因為我是先大致瀏覽了一遍官方文件,然後做的學習計劃,結果發現經過前面的學習,對Kotlin的語法掌握了的情況下,其實很多Java和kotlin之間的區別都已經很熟悉了,強烈推薦大家去看看 From Java To Kotlin,就當是一個複習/總結,加深對Kotlin語法的熟悉。

明天開始正式進入Kotlin in Android 的學習,這個才是我們Kotlin學習之旅的重點。

Day 8 - Learn Kotlin Trip, Completed.