KOTLIN, the Latest on Mobile App Development

Initially, to create mobile apps on Android, Java was the only programming language. Now developers can choose to use Java or Kotlin, the new Google official language for the creation of mobile applications on Android. Though it’s still possible to program code in Java and Kotlin in the same project, this update is beneficial because you can migrate your job little by little without affecting the performance of the app.

Do you like this content? We’re looking for you! JOIN OUR TEAM and share your knowledge to build the future of telecommunications.

Kotlin solves many of the problems that Java has without too much complexity. It is a small language that can use all the libraries offered by the JVM without problems. With this scenario, using Kotlin does not seem to be so risky.

How to create a Kotlin application from Android Studio

 To generate a new Kotlin application from Android Studio, we must do the following: File -> New -> New Project.

When we select New Project, a new window opens where we must:

  • Place the name of the application.
  • Place the domain.
  • Select the Kotlin support.
  • Select Phone and Tablet.

Then we select basic activity and finalize.

Why use Kotlin on Android?

The multiple reasons to develop mobile applications with this tool show us its simplicity and speedy workflow. They are:

Lambdas

In Java 8 the use of lambdas has been well known, for example, when adding the OnClickListener to an Android button we extended the abstract class and overwrote a method. However, in other languages such as Javascript, this is solved by passing a function as a parameter. Given this scenario, Kotlin implements the same functionality as Javascript.

 

Extend System Classes or Libraries

Kotlin allows us to add methods and functions to classes and external libraries, such as Android classes. This function will enable us to use some methods that could be called by infix notation. The process gives us the appearance that the methods belong to the class.

public fun android.view.View.onClick(l: (v: android.view.View) -> Unit): Unit = setOnClickListener(l)

Null Safety

Kotlin was born to solve common problems that were in Java. For example, when there are null pointer exceptions, Kotlin does not allow to start any variable in null unless the variable is declared as nullable. It also requires us to use the variables claimed as nullable, to avoid null.

public var nullableString: String? = null

public var nonNullableString: String = null

public var nonNullableString: String

Only in the first case would not give an error, because it is the correct way to declare it as nullable.

Data Classes

Kotlin introduces the concept of Data Classes, which represents simple classes used as data containers and does not encapsulate any additional logic. Note that Kotlin does not use getting and setting to recover and assign data.

data class User(val name: String, val lastName: String, val age: Int)

When Expression

The expression Java switch is too limited. Kotlin uses expressions when they can do everything that makes the switch and can also add if / else, which can have a more complex code.

when(view.visibility){

    View.VISIBLE -> toast("visible")

    View.INVISIBLE -> toast("invisible")

    else -> toast("gone")

}

Interoperability Between Kotlin and Java

Kotlin maintains interoperability with Java. These transactions, allow us to use the vast majority of existing libraries without any problems.

As we can see the use of Kotlin in Android was born to correct some aspects that Java couldn’t handle. Such are the cases of the null pointer exception, the use of simple classes and also the use of enriched. In the present material, it is that the import is done automatically. As a developer, I can conclude that the use of Kotlin in Android is an excellent option and it provides us with many improvements that Java did not support before.

Do you like this content? We’re looking for you! JOIN OUR TEAM and share your knowledge to build the future of telecommunications.

Menu