Kotlin เป็น breakthrough ใหญ่ 2 ปีที่ผ่านมา เป็น trending topic ทุกที่ และยังคงยอดนิยมเพิ่มขึ้น Google ยืนยันอย่างเป็นทางการว่า Kotlin เป็นภาษาอย่างเป็นทางการสำหรับ Android App Development
แต่มากกว่านั้น คนเยอะยังชอบ Java มากกว่า Kotlin สำหรับ Android development ทำไม? เหตุผลหลักคือ คนยังไม่สบายใจเปลี่ยนภาษาหลักจาก Java ไป Kotlin คนกลัวการเปลี่ยนแปลงสิ่งใหม่
เริ่มใช้ Kotlin ตอนนี้ วิธีง่ายๆ
ความเป็นจริง การ switch จาก Java ไป Kotlin ง่ายมาก เป็นภาษายืดหยุ่น เขียน Kotlin ได้ แต่แบบ idiomatic มันคือ Java!

นี่คือสิ่งที่ควรทราบเมื่อเปลี่ยนจาก Java ไป Kotlin:
1. Java Interoperability
Kotlin interoperable กับ Java เต็มที่ และในทางกลับกัน หมายความว่า Kotlin เรียก Java code ได้ ทำให้ migration ง่ายมาก เขียน Java ไปครึ่งทางแล้ว? ไม่เป็นไร เขียน Kotlin ต่อได้เลย
2. ไม่มี primitive data types แล้ว
จำได้ไหม ตอนเลือก int หรือ Int ใน Java ใช้เวลาครึ่งวัน? ตอนนี้ไม่มี primitive data types ใน Kotlin ทุกอย่าง wrapped ใน Object แทน
3. Syntax เปลี่ยนเล็กน้อย (แต่ไม่ต้องกังวล เรียนรู้ง่าย)
Syntax บางอย่าง เช่น การประกาศ variable, function, class เปลี่ยนเล็กน้อย แต่ถ้ามาจาก OOP background เช่น Java จะไม่ใช่ปัญหา และ void keyword และ semicolon (';') ถูกลบออก ขอบคุณ Kotlin ที่ลบ semicolon!
นี่คือตัวอย่างพื้นฐานกับ Android class:
//Inheritance และ implementation ใช้ ':' ตอนนี้ class BasicActivity : AppCompatActivity() { //variable declaration ตอนนี้ชื่อ variable ก่อนแล้วค่อย type var a :Int = 10 //var สำหรับ mutable val b :Int = 15 //val สำหรับ immutable /*lateinit keyword ต้องเพิ่มสำหรับ initialization non-primitive type โดยไม่มีค่าเริ่มต้นใน Kotlin*/ lateinit var someTextView:TextView //ไม่มี new keyword แล้ว var gameList:List<Integer> = ArrayList() //overriding ใช้ keyword override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_basic) } } 4. Type inference
Kotlin ทำให้ชีวิตง่ายด้วย type inference สำหรับ variables:
var a = 5 //จะ infer อัตโนมัติว่าเป็น integer var b = "John" //จะ infer อัตโนมัติว่าเป็น String 5. Null safety checks
Kotlin มี null safety checking ป้องกัน null pointer exception ทุก variable ที่อาจคืนค่า null จะต้อง mark (และต้อง mark) ด้วยเครื่องหมาย ?
var a:String? = null var b:String = null //not allowed ยังมี null safety call ป้องกัน Null-Pointer Exception (Java Billion Dollar Mistake)
//not allowed, a อาจเป็น null a.function() //นี้ไม่โยน null pointer แต่อาจทำให้ flow ดำเนินต่อ a?.function() //force function call อาจก่อให้เกิด NPE ใช้ถ้าแน่ใจ 100% ว่าค่านี้ไม่ใช่ null a!!.function()6. ไม่มี static keyword แล้ว
Static keyword ถูกแทนที่ด้วย Object keyword ทั้ง classes และ methods โดยเฉพาะ Companion Object สำหรับ methods
7. Switch case ปรับเปลี่ยน
Switch case เปลี่ยนเป็น keyword ใหม่ชื่อ when ดูสะอาดและยืดหยุ่นกว่า:
when (x) { 1 -> println("One") 2 -> print("Two") else -> print("Others") }8. ไม่มี checked exception แล้ว
จำได้ไหม ตอนทำ operations หรือ casting ใน Java แล้ว red warning กระโดดออกมาบอกให้เพิ่ม exception checking? ถูกลบใน Kotlin แล้ว IDE ไม่บังคับให้ทำ exception checking อีกต่อไป!!
นั่นคือส่วนที่ต้องการเริ่มพัฒนาใน Kotlin แบบ idiomatic Java ไม่ยากเลยที่จะเข้า Kotlin ตอนนี้ ถูกต้องไหม? ตอนนี้ Kotlin ไม่ดูต่างจาก Java มากแล้ว
ฟีเจอร์ที่ Kotlin ให้มา

หลังดูความแตกต่าง Kotlin และ Java ต้องดูฟีเจอร์ที่ Kotlin ให้ และวิธีเริ่มทำ Kotlin แบบ Kotlin way:
1. Flexibility
เหตุผลหลักที่ผมชอบ Kotlin คือความยืดหยุ่นของภาษา คุณ OOP purist? อยากลอง Functional Programming ที่ hot topic? และเพื่อคุณที่รัก coding คุณทำทั้งสองได้ใน Kotlin! แม้ไม่ให้ FP features ครบ แต่เพียงพอรองรับ
2. Lambda Support
รู้ว่า Java 8 รองรับ lambda แล้ว แต่ Kotlin มาก่อน และทำได้ดีมาก แม้ transition ไปใช้ Lambda Function จะยากตอนแรก แต่ Kotlin ยืดหยุ่นบนเรื่องนี้ เลือกคุณเลย!
Android library บางอย่าง enable Lambda support เช่น View.OnClickListener (มาจาก Android background ตัวนี้คุ้นเคยมาก):
val randomButton : Button //ใช้ lambda function เป็น argument randomButton.setOnClickListener{v -> doRandomStuff(v) }3. Data Class
Kotlin ให้ substitute สำหรับ Model/Entity class เรียกว่า Data class ลบความต้องการ setter getter ซ้ำซ้อน และมี in-built equal method และ toString function โดยไม่ต้องสร้างเอง ใช้งานง่ายมาก:
data class Person( val id:String, val name:String = "John Doe" //Default Value ) //Initialisation block var person = Person("id","Not John")4. Extension Functions
Kotlin อนุญาต extension functions ทำให้โค้ดอ่านง่ายขึ้น ยังช่วยให้เพิ่ม functionality ให้ class โดยไม่ต้อง modify class เอง:
fun Int.superOperation(anotherInt:Int):Int { val superNumber = this * anotherInt + anotherInt return superNumber } //ตอนนี้เรียก function ได้ val someNumber = 5 val superNumber = someNumber.superOperation(10) //655. Named and Default Arguments
เหมือน C# Kotlin รองรับ named และ default parameters สำหรับ methods ลบความต้องการส่งค่าในทุก arguments เลือกได้ว่าจะ modify ค่าไหน แค่นั้น! ไม่ต้องส่งค่าเดิมซ้ำ 10 ครั้งใน function calls!
//variable declaration fun someOperations(var x: Int, var y:Int, var z:Int = 1){ return x+y+z } someOperations(1,2) //return 4 someOpeartions(y = 1, x = 1) //return 36. Referential Equality
Kotlin มี referential equality ('===') ตรวจสอบว่า variable 2 ตัว refer ไป object เดียวกันหรือไม่
var a = 10 var b = 10 a == b //true a === b //true also, primitive type object equation จะเช็คแค่ค่า data class Person (val name: String) val p1 = Person("John") val p2 = Person("John") p1 == p2 //true p1 === p2 //false7. Smart casting
แทน instance of Kotlin ให้ syntax ง่ายและอ่านง่ายสำหรับ casting หรือ type checking:
//type checking //kotlin smart cast mechanism จะ auto cast object เป็น type ที่ตรงกันถ้าผ่าน type checking if (shape is Circle) { println(“it’s a Circle”) shape.radius //auto cast เป็น Circle } else if (shapeObject is Square) { println(“it’s a Square”) } else if (shapeObject is Rectangle) { print(“it’s a Rectangle”) }Kotlin มี as keyword สำหรับ explicit casting:
//ถือว่า unsafe เพราะพฤติกรรมคล้าย static casting var anotherShape = shape as Circle //ใช้แทนได้สำหรับ safe casting var safeShape = shape as? Circle8. Coroutines
Asynchronous call ยุ่งยากใน Java การทำ Thread ใช้โค้ดเยอะ ทำให้ unreadable Coroutines สร้างมาแทน traditional thread operations ให้ clean และ robust code:
import kotlinx.coroutines.* fun main() { // launch coroutine ใหม่ใน background และดำเนินต่อ GlobalScope.launch { // non-blocking delay 1 วินาที (default time unit ms) delay(1000L) // print หลัง delay println("World!") } // main thread ทำงานต่อขณะ coroutine delayed println("Hello,") // block main thread 2 วินาทีเก็บ JVM alive Thread.sleep(2000L) }Coroutines รองรับ complex operations เช่น Job Joining, Channel, Shared Context และ Parents รายละเอียดและ usage เพิ่มเติมอ่านได้ที่นี่
สรุป
สรุปคือ เริ่มใช้ Kotlin ง่ายมาก เขียน Kotlin คล้าย Java เกือบทุกอย่าง และ Kotlin ให้ฟีเจอร์หลายอย่างที่ให้ข้อได้เปรียบเหนือ Java
แน่ใจว่า Java ยังแข็งแกร่งอยู่ แต่ไม่ผิดที่จะเริ่มเรียนรู้สิ่งใหม่ เราเป็นโปรแกรมเมอร์ ต้องยอมรับ lifetime learning และไม่หยุดเรียนรู้ จุดเด่นของ Kotlin คือ migrate จาก Java ง่ายมาก
ขอบคุณที่อ่าน หวังว่าเพลิดเพลินและมีประโยชน์ โชคดีกับการสำรวจ Kotlin เจอกันใหม่ครั้งหน้า