Let’s discover Kotlin — Part 1

What is Kotlin ?

It’s a new free and open source programming language targeting the Java platform and it is fully interoperable with Java. It can be used almost everywhere Java is used today (server-side, Android app …).

Kotlin is a statically typed programming language. This means that errors are reported and the type of every expression is known at compile time.

Kotlin doesn’t require you to specify the type of every variable explicitly in your source code (the type of a variable can automatically be determined).

val year = 2017

This variable is automatically declared as an Int, this mecanism is called Type Inference.

To develop in Kotlin you can use Intellij IDEA or Android Studio (for Android apps).

Functions

Let’s start with a simple example :

fun hello() {
    println("Hello World!")
}

In this example :

  • You use fun keyword to declare the hello function.
  • You use println method to print a message on the standard output.

Let’s go further in function declaration

fun add(a: Int, b: Int): Int {
    return a + b
}

This function called add has two Int values in parameters (a and b) and returns the sum of these two values. The return type is declared after the function parameters.

You can now simplify the function by removing

  • Braces,
  • Return statement,
  • And the return type (automatically determined).
fun add(a: Int, b: Int) = a + b

Variables

If we take the first example seen before

val year = 2017

This is equivalent to :

val year: Int = 2017

You can declare two types of variables (mutable and immutable) :

  • Immutable variable : declared with the val keyword. It means that this variable can’t be reassigned after initialization.
  • Mutable variable : declared with the var keyword. The value can be changed.

The advice is to declare as much as possible you variable as Immutable, this avoid side effects in your code.

Classes

For example, let’s declare a User class with two fields (usename and password)

class User(val login: String, var password: String)

The declaration is composed of :

  • class keyword,
  • The name of the class,
  • You can now declare the properties of the class (like declaring variable).
  • public is default visibility of a class (so you can avoid to put it).
  • A property declared with val is a read-only variable (generating the property and a getter).
  • A property declared with var can be modified (generating the property, a setter and a getter).

If we declare and compile the following code :

class User(val login: String, var password: String)

fun main(args: Array<String>) {
    val user = User("nazim", "password")
    println(user.login);
    println(user.password);
    user.login = "nazim2"
    user.password = "password2"
    println(user.password)
}

The following compilation error will appear Val cannot be reassigned, because we are trying to set a new value for login field.

You can also declare custom accessors. For example, we declare an isSafePassword variable (to verify is a password is safe) and declare an accessor to get the value of this variable.

class User(val login: String, var password: String) {
    val isSafePassword: Boolean
    get() {
        return passoword.length > 7
    }
}

fun main(args: Array<String>) {
    val user = User("nazim", "password")
    println(user.login);
    println(user.password);
    user.password = "password2"
    println(user.password)
    println(user.isSafePassword)
}

Iterating

Iteration in Kotlin is similar to Java. The while loop (and do-while) are the same as in Java :

var i = 100
while (i > 0) {
    i--
}

and for loop can be used as “for-each” in java :

for (item in collection) print(item)

This is the first part of these tutorials about Kotlin, You can find the second part here.

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *