SlideShare a Scribd company logo
1 of 38
Kotlin Crash Course 101
Kyle Lin
@ChAoSUnItY
Part 1: Introduction & Syntax
甚麼是Kotlin
一個俄羅斯島嶼 - 科特林島
甚麼是Kotlin
一個程式語言 - Kotlin Programming Language
甚麼是Kotlin
特色 Features
- JVM 語言 (編譯語言)
- 可與 Java 互相使用
- Null 安全性提高
- 較Java簡潔
甚麼是Kotlin
為甚麼該學Kotlin
- Google 於 Google I/O 2017 上宣布Android開發將以
Kotlin為優先
- 更自由開放的程式風格
- 開發上直覺且快速
Intellij IDEA
工欲善其事,必先利其器
- IDE (整合式開發環境)
- 支援Java、Scala、以及Kotlin
- 強力的Code completion以及Project Management
Intellij IDEA
安裝
- https://www.jetbrains.com/idea/
Intellij IDEA
安裝
- 點擊”Download”
- 根據作業系統選擇下載檔(左上)
- 選擇右方社群版
- 根據指示安裝
- 安裝好後開啟
Intellij IDEA
安裝完畢
Intellij IDEA
建立編輯環境
- 點擊”New Project”
Intellij IDEA
建立編輯環境
- 名稱任意
- 語言: Kotlin
- 建置系統:Intellij
- 記得勾選”Add sample
code”!!!
Intellij IDEA
建立編輯環境
- JDK選擇
- 若未安裝過JDK,
點擊”Download JDK…”。
點擊後選擇版本17,Vendor任意
- 若已安裝過,選擇任意版本即可
Intellij IDEA
建立完畢
萬丈高樓平地起
Kotlin語法 - 基本型別
萬丈高樓平地起
- Kotlin沒有原生資料型別 (Primitive data type)
- Kotlin的數字資料型別 (Numerical data type) 有著共
同的父類別Number
Kotlin語法 - 基本型別
// Java primitive types || Kotlin primitive types
// byte || Byte
// Short || Short
// int || Int
// long || Long
// float || Float
// double || Double
// T[] (Array) || Array<T>
Var v.s Val - 我們不一樣
- var 用來宣告可變變數 (mutable variable)
- val 用來宣告不可變變數 (immutable variable)
Kotlin語法 - 變數宣告
fun main(args: Array<String>) {
var a = 1
a = 2 // Compile Success
val b = 1
b = 2 // Compile error: Val cannot be reassign
}
Var v.s Val - 我們不一樣
- 變數宣告採用型別推斷 (Type Inference)
- 若編譯器無法正確推斷、或是要增強可讀性,也可以
自行補上型別
Kotlin語法 - 變數宣告
fun main(args: Array<String>) {
val a = 1 // Actual type: Int
val b: Int = 1 // Actual type: Int
}
Return - 回傳或結束
- Return 用法和 Java 或 C 一樣
- Return 需依照函式的定義回傳相同型別
Kotlin語法 - 控制流
If-Else - 更靈活的判斷式
- If-else 用法和 Java 或 C 一樣
- If-else 在 Kotlin 裡是一個表達式 (Expression)
- 可以看作三元運算子 (Ternary Operator)
Kotlin語法 - 判斷式
fun max(a: Int, b: Int): Int {
return if (a > b) a else b
}
int max(int a, int b) {
return a > b ? a : b;
}
When - 史詩級強化的Switch-case
- 相當於 Java 裡增強版的 Switch-case
- When 在Kotlin裡是一個表達式 (Expression)
- 不允許條件級聯 (Case Cascading),但允許多重判斷
- When 可以判斷多種型別:
- 數字 (Byte, Short, Int, Long, Float, Double)
- 字串 (String)
- 布林 (Boolean)
- 以及更多!
Kotlin語法 - 判斷式
fun check(a: Int) {
when (a) {
0 -> println(“a == 0”)
1 -> println(“a == 1”)
else -> println(“a != 1 && a != 0”)
}
}
void check(int a) {
switch (a) {
case 0:
System.out.println(“a ==
0”);
break;
case 1:
System.out.println(“a ==
1”);
break;
default:
System.out.println(“a != 1
&& a != 0”);
break;
}
}
fun check(a: Int) {
when {
a == 0 -> println(“a == 0”)
a == 1 -> println(“a == 1”)
else -> println(“a != 1 && a != 0”)
}
}
void check(int a) {
switch (a) {
case 0:
System.out.println(“a ==
0”);
break;
case 1:
System.out.println(“a ==
1”);
break;
default:
System.out.println(“a != 1
&& a != 0”);
break;
}
}
fun check(a: Object) {
when (a) {
is String -> println(“a is
String”)
else -> println(“a is something else”)
}
}
void check(Object a) {
if (a instanceof String) {
System.out.println(“a is String”);
} else {
System.out.println(“a is something
else”);
}
}
While & Do-While - 先判斷還是先執行?
- While 和 Do-while 用法和 Java 或 C 一樣
- 兩者差異在於:
- While 先判斷條件再執行內容
- Do-while 先執行內容一次後再判斷
Kotlin語法 - 迴圈
fun loop() {
println(“Enter loop”)
while (1 < 0) {
println(“Executed”)
}
}
// Output:
// Enter loop
fun loop() {
println(“Enter loop”)
do {
println(“Executed”)
} while (1 < 0)
}
// Output:
// Enter loop
// Executed
For - 給定一範圍,迭代於該範圍內
- For 接受迭代器 (Iterator) 作為範圍
- 迭代器可以是:
- 數字範圍
- 字串
- 可迭代的資料結構
Kotlin語法 - 迴圈
範圍表達式 - 直觀且簡潔
- 範圍表達式 (Ranged Expression) 寫法如下:
- 令 i 為索引, lb 為下界, ub 為上界,則:
- lb .. ub:範圍為 lb <= i <= ub
- lb until ub:範圍為 lb <= i < ub
- ub downTo lb:範圍為 lb <= i <= ub,從 ub 往 lb 索引
- lb .. ub step 2:範圍為 lb <= i <= ub,且每次跳過1次
索引
Kotlin語法 - 迴圈
fun loop() {
for (i in 0 .. 100) {
println(i)
}
}
void loop() {
for (int i = 0; i <= 100; i++) {
System.out.println(i);
}
fun loop() {
for (i in 0 until 100) {
println(i)
}
}
void loop() {
for (int i = 0; i < 100; i++) {
System.out.println(i);
}
fun loop() {
for (i in 0 downTo 100) {
println(i)
}
}
void loop() {
for (int i = 100; i >= 0; i--) {
System.out.println(i);
}
fun loop() {
for (i in 0 .. 100 step 2) {
println(i)
}
}
void loop() {
for (int i = 0; i < 100; i += 2) {
System.out.println(i);
}
Leetcode - 1952. Three Divisors
- 給定一個數字n,判斷n的因數是否剛剛好有3個
- Example 1:
Input:
n = 3
Output:
false
Note: divisors = [1, 3]
Wrap up - 挑戰時間!
Leetcode - 1952. Three Divisors
- Example 2:
Input:
n = 4
Output:
true
Note: divisors = [1, 2, 4]
Wrap up - 挑戰時間!
class Solution {
fun isThree(n: Int): Boolean {
var counter = 0
for (i in 1 .. n) {
if (n % i == 0) {
counter++
}
if (counter > 3) {
return false
}
}
return counter == 3
}
}

More Related Content

More from FCUGDSC

GDSC FCU 第1堂 前端新手村 - HTML, CSS, JavaScript介紹
GDSC FCU 第1堂 前端新手村 - HTML, CSS, JavaScript介紹GDSC FCU 第1堂 前端新手村 - HTML, CSS, JavaScript介紹
GDSC FCU 第1堂 前端新手村 - HTML, CSS, JavaScript介紹FCUGDSC
 
GDSC FCU 第2堂 Kotlin
GDSC FCU 第2堂 KotlinGDSC FCU 第2堂 Kotlin
GDSC FCU 第2堂 KotlinFCUGDSC
 
GDSC FCU 第3堂 Flutter
GDSC FCU 第3堂 FlutterGDSC FCU 第3堂 Flutter
GDSC FCU 第3堂 FlutterFCUGDSC
 
GDSC FCU 第2堂 Flutter
GDSC FCU 第2堂 FlutterGDSC FCU 第2堂 Flutter
GDSC FCU 第2堂 FlutterFCUGDSC
 
flutter installation.pptx
flutter installation.pptxflutter installation.pptx
flutter installation.pptxFCUGDSC
 
flutter introduction .pptx
flutter introduction .pptxflutter introduction .pptx
flutter introduction .pptxFCUGDSC
 

More from FCUGDSC (6)

GDSC FCU 第1堂 前端新手村 - HTML, CSS, JavaScript介紹
GDSC FCU 第1堂 前端新手村 - HTML, CSS, JavaScript介紹GDSC FCU 第1堂 前端新手村 - HTML, CSS, JavaScript介紹
GDSC FCU 第1堂 前端新手村 - HTML, CSS, JavaScript介紹
 
GDSC FCU 第2堂 Kotlin
GDSC FCU 第2堂 KotlinGDSC FCU 第2堂 Kotlin
GDSC FCU 第2堂 Kotlin
 
GDSC FCU 第3堂 Flutter
GDSC FCU 第3堂 FlutterGDSC FCU 第3堂 Flutter
GDSC FCU 第3堂 Flutter
 
GDSC FCU 第2堂 Flutter
GDSC FCU 第2堂 FlutterGDSC FCU 第2堂 Flutter
GDSC FCU 第2堂 Flutter
 
flutter installation.pptx
flutter installation.pptxflutter installation.pptx
flutter installation.pptx
 
flutter introduction .pptx
flutter introduction .pptxflutter introduction .pptx
flutter introduction .pptx
 

GDSC FCU 第1堂 Kotlin

  • 1. Kotlin Crash Course 101 Kyle Lin @ChAoSUnItY Part 1: Introduction & Syntax
  • 4. 甚麼是Kotlin 特色 Features - JVM 語言 (編譯語言) - 可與 Java 互相使用 - Null 安全性提高 - 較Java簡潔
  • 5. 甚麼是Kotlin 為甚麼該學Kotlin - Google 於 Google I/O 2017 上宣布Android開發將以 Kotlin為優先 - 更自由開放的程式風格 - 開發上直覺且快速
  • 6. Intellij IDEA 工欲善其事,必先利其器 - IDE (整合式開發環境) - 支援Java、Scala、以及Kotlin - 強力的Code completion以及Project Management
  • 8. Intellij IDEA 安裝 - 點擊”Download” - 根據作業系統選擇下載檔(左上) - 選擇右方社群版 - 根據指示安裝 - 安裝好後開啟
  • 11. Intellij IDEA 建立編輯環境 - 名稱任意 - 語言: Kotlin - 建置系統:Intellij - 記得勾選”Add sample code”!!!
  • 12. Intellij IDEA 建立編輯環境 - JDK選擇 - 若未安裝過JDK, 點擊”Download JDK…”。 點擊後選擇版本17,Vendor任意 - 若已安裝過,選擇任意版本即可
  • 15. 萬丈高樓平地起 - Kotlin沒有原生資料型別 (Primitive data type) - Kotlin的數字資料型別 (Numerical data type) 有著共 同的父類別Number Kotlin語法 - 基本型別
  • 16. // Java primitive types || Kotlin primitive types // byte || Byte // Short || Short // int || Int // long || Long // float || Float // double || Double // T[] (Array) || Array<T>
  • 17. Var v.s Val - 我們不一樣 - var 用來宣告可變變數 (mutable variable) - val 用來宣告不可變變數 (immutable variable) Kotlin語法 - 變數宣告
  • 18. fun main(args: Array<String>) { var a = 1 a = 2 // Compile Success val b = 1 b = 2 // Compile error: Val cannot be reassign }
  • 19. Var v.s Val - 我們不一樣 - 變數宣告採用型別推斷 (Type Inference) - 若編譯器無法正確推斷、或是要增強可讀性,也可以 自行補上型別 Kotlin語法 - 變數宣告
  • 20. fun main(args: Array<String>) { val a = 1 // Actual type: Int val b: Int = 1 // Actual type: Int }
  • 21. Return - 回傳或結束 - Return 用法和 Java 或 C 一樣 - Return 需依照函式的定義回傳相同型別 Kotlin語法 - 控制流
  • 22. If-Else - 更靈活的判斷式 - If-else 用法和 Java 或 C 一樣 - If-else 在 Kotlin 裡是一個表達式 (Expression) - 可以看作三元運算子 (Ternary Operator) Kotlin語法 - 判斷式
  • 23. fun max(a: Int, b: Int): Int { return if (a > b) a else b } int max(int a, int b) { return a > b ? a : b; }
  • 24. When - 史詩級強化的Switch-case - 相當於 Java 裡增強版的 Switch-case - When 在Kotlin裡是一個表達式 (Expression) - 不允許條件級聯 (Case Cascading),但允許多重判斷 - When 可以判斷多種型別: - 數字 (Byte, Short, Int, Long, Float, Double) - 字串 (String) - 布林 (Boolean) - 以及更多! Kotlin語法 - 判斷式
  • 25. fun check(a: Int) { when (a) { 0 -> println(“a == 0”) 1 -> println(“a == 1”) else -> println(“a != 1 && a != 0”) } } void check(int a) { switch (a) { case 0: System.out.println(“a == 0”); break; case 1: System.out.println(“a == 1”); break; default: System.out.println(“a != 1 && a != 0”); break; } }
  • 26. fun check(a: Int) { when { a == 0 -> println(“a == 0”) a == 1 -> println(“a == 1”) else -> println(“a != 1 && a != 0”) } } void check(int a) { switch (a) { case 0: System.out.println(“a == 0”); break; case 1: System.out.println(“a == 1”); break; default: System.out.println(“a != 1 && a != 0”); break; } }
  • 27. fun check(a: Object) { when (a) { is String -> println(“a is String”) else -> println(“a is something else”) } } void check(Object a) { if (a instanceof String) { System.out.println(“a is String”); } else { System.out.println(“a is something else”); } }
  • 28. While & Do-While - 先判斷還是先執行? - While 和 Do-while 用法和 Java 或 C 一樣 - 兩者差異在於: - While 先判斷條件再執行內容 - Do-while 先執行內容一次後再判斷 Kotlin語法 - 迴圈
  • 29. fun loop() { println(“Enter loop”) while (1 < 0) { println(“Executed”) } } // Output: // Enter loop fun loop() { println(“Enter loop”) do { println(“Executed”) } while (1 < 0) } // Output: // Enter loop // Executed
  • 30. For - 給定一範圍,迭代於該範圍內 - For 接受迭代器 (Iterator) 作為範圍 - 迭代器可以是: - 數字範圍 - 字串 - 可迭代的資料結構 Kotlin語法 - 迴圈
  • 31. 範圍表達式 - 直觀且簡潔 - 範圍表達式 (Ranged Expression) 寫法如下: - 令 i 為索引, lb 為下界, ub 為上界,則: - lb .. ub:範圍為 lb <= i <= ub - lb until ub:範圍為 lb <= i < ub - ub downTo lb:範圍為 lb <= i <= ub,從 ub 往 lb 索引 - lb .. ub step 2:範圍為 lb <= i <= ub,且每次跳過1次 索引 Kotlin語法 - 迴圈
  • 32. fun loop() { for (i in 0 .. 100) { println(i) } } void loop() { for (int i = 0; i <= 100; i++) { System.out.println(i); }
  • 33. fun loop() { for (i in 0 until 100) { println(i) } } void loop() { for (int i = 0; i < 100; i++) { System.out.println(i); }
  • 34. fun loop() { for (i in 0 downTo 100) { println(i) } } void loop() { for (int i = 100; i >= 0; i--) { System.out.println(i); }
  • 35. fun loop() { for (i in 0 .. 100 step 2) { println(i) } } void loop() { for (int i = 0; i < 100; i += 2) { System.out.println(i); }
  • 36. Leetcode - 1952. Three Divisors - 給定一個數字n,判斷n的因數是否剛剛好有3個 - Example 1: Input: n = 3 Output: false Note: divisors = [1, 3] Wrap up - 挑戰時間!
  • 37. Leetcode - 1952. Three Divisors - Example 2: Input: n = 4 Output: true Note: divisors = [1, 2, 4] Wrap up - 挑戰時間!
  • 38. class Solution { fun isThree(n: Int): Boolean { var counter = 0 for (i in 1 .. n) { if (n % i == 0) { counter++ } if (counter > 3) { return false } } return counter == 3 } }