- Published on
Android Kotlin 101: Basic Knowledge
- Authors
- Name
- Dan Tech
- @dan_0xff
Introduction
Kotlin is a modern programming language that was 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.
Applications of Kotlin in the world of programming
- Kotlin is the primary language for Android mobile application development. Why only Android mobile applications? Because AOSP projects still use Java a lot. Also, the View, Activity, Fragment... libraries of Android Core are still being maintained in Java.
Kotlin can run on many platforms: Android, iOS, Web (Web asm), Windows Desktop, Ubuntu, Mac, Backend (JVM) → https://www.jetbrains.com/kotlin-multiplatform/
Kotlin can also be used in Data Analysis → https://kotlinlang.org/docs/data-analysis-overview.html
Useful features of Kotlin
Mutable and Immutable
- Mutable and Immutable → Is a data type to determine whether a variable can change the reference value of a variable or not (Need to check the document in this section). 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, although it cannot be assigned a new value, can still change its elements. To ensure that your List cannot be changed throughout the execution of the program, you can use the List data type
NULL Safety
- 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
- late init var → is a way to declare a variable using the var keyword, but its initialization value is not yet known. The initialization value will be created through an assignment at a different point in the running program. If the variable is accessed before the value is declared, the program will encounter an Exception, and there is a possibility of an error causing a crash. Note: the variable declared late init var must be non nullable
by lazy val
- by lazy val → is a way to declare a variable using the val keyword, the initialization value of by lazy is not yet known, but the initialization method is defined in the accompanying function (see example). 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
- 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 (see example)
Operators
- 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 (see example)
Future career 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
Good luck!