Published on

Kotlin - Modern Programming Language for Android and Multiplatform

Authors

Introduction

Kotlin is a modern programming language first announced in 2011. The first stable version of Kotlin was released in 2016. Kotlin was officially recognized by Google as the Android programming language in 2019.

Kotlin's applications in the programming world

  • Kotlin is the primary language for Android mobile application development

    • Why only Android mobile applications? Because AOSP projects still use Java a lot. At the same time, the View, Activity, Fragment... libraries of Android Core are still being maintained in Java.
  • Kotlin is a language that can be compiled on multiple platforms: Android, iOS, Web (Web asm), Windows Desktop, Ubuntu, Mac, Backend (JVM) → Kotlin Multiplatform

  • Kotlin can also be used in Data AnalysisKotlin for Data Analysis

Useful features of Kotlin

  • Mutable and Immutable → It is a data type to determine whether a variable can change the reference value of a variable or not (Need to check the document here).

    • To declare an Immutable variable, we use the keyword val
    • To declare a Mutable variable, we use the keyword var
    • Note that a val MutableList cannot be assigned a new value, but its elements can still be changed. To ensure that your List cannot be changed during program execution, you can use the List data type
  • Null safety → To decide whether a variable is Non-Null or Nullable, we determine its type from the beginning when declaring it in a class or function using the ? operator.

  • late init var → is a form of declaring a variable using the keyword var, but its initialization value is not yet known. The initialization value will be created through an assignment at a different time of the running program. If the variable is accessed before declaring the value, the program will encounter an Exception, and there is a possibility of a crash.

    • Note: the variable declared late init var must be non nullable
  • by lazy val → is a form of declaring a variable using the keyword val, the initialization value of by lazy is not yet known, but the initialization method has been defined in the accompanying function. When the value of a variable declared with by lazy val is accessed for the first time, the accompanying function will be executed only once and return the value of the variable. From the second call onwards, the variable will directly access this value without having to re-execute the accompanying function.

  • Extension function → This is a useful feature of Kotlin. It helps programmers write extension functions for an existing data type without directly extending that data type.

// Extension function example
fun String.removeSpaces(): String {
    return this.replace(" ", "")
}

// Usage
val text = "Hello World"
val result = text.removeSpaces() // "HelloWorld"
  • Operator !! → This operator is used to cast a nullable variable to non-nullable. Programmers need to be very careful when using it to avoid unwanted program crashes.

  • Operator ?: → This is a conditional operator, often used to get the fallback value of a nullable variable.

// Elvis operator example
val result = nullableValue ?: "default value"

Career future with Kotlin

Kotlin is a versatile language, mastering Kotlin will increase the flexibility of your career in the future.

  • Become an Android Software Engineer
  • Become a Backend Engineer
  • FullStack Software Engineer

Conclusion

Kotlin is not only the official programming language for Android but also a powerful tool for cross-platform development. With modern features like null safety, extension functions, and good interoperability with Java, Kotlin is becoming the top choice for many developers.


This article introduces the basic concepts of Kotlin. To learn more, you can refer to the official Kotlin documentation.