1 - Declarations of variables In Kotlin

we distinguish the variables whose value we can change, and the immutable variables that are not quite constants: the keyword val allows to declare an immutable variable, the var keyword is used to declare an alterable variable.

val name: String = "Michael"
// name = "Walid" // Forbidden !!! Because name has been declared with val
var age = 5
age + = 10 // no problem because age is alterable.

As a reminder, we must specify the type of variable after the name, not before as in Java. Obviously, an immutable variable must be initialized when it is declared. This works equally well for "primitive" types (everything is object in Kotlin) as for personal types.

class Person (val name: String, var age: Int = 10)

val Claude: Person = new Person ("Jim", 25)

Even if classes in Kotlin will be explained later, here the Person class has a read-only name property, and a read-write age property, which defaults to 10. (Note however, here the class Nobody has not defined a modifying method, but if it were, it would also have been probable that the variable Claude could see its modified state even though it was declared as val! Here the word -val val just prevents the object pointed by Claude from changing.)

2 - Basic types

The Byte, Short, Int, Long, Float, and Double numeric types are equivalent to the existing types in Java: except that, as a reminder, in Kotlin, everything is object. So we can write:

3.toString ()
7.downTo (0) // generates the range (ie the interval) decreasing from 7 to 0.

The Boolean type also works as in Java:

val under18 = age <18
val isLucas = name == "Lucas"
val iFoundHim = under18 && isLucas

The String type has the same methods as in Java, but also additional methods (complete list here). So we can write:

println ("martin" .all {it.isLowerCase ()}) // Test if the whole word is lowercase.

The all method, which tests whether all the elements of a collection fulfill a criterion, does not exist in Java: it is an extension of the Kotlin language. The piece of code constituted by the braces is an anonymous function (or lambda): it will be discussed later. Kotlin also adds the type Range (interval), very useful. Thus one can execute a loop thanks to a Range object (indeed, the basic Java syntax to perform a loop does not exist in Kotlin):

for (i in 3..7) {
    println (i)
} // Show numbers from 3 to 7 on multiple lines.

But we can also get a descending interval with downTo:

for (i in 10 downTo 0) {
    println (i)
} // Count down from 10 to 0, on multiple lines.
It is also possible to specify a progression other than 1:
for (i in 2..36 step 3) println (i) // Display the numbers 2,5,8, ..., 35

It is also possible to combine a descending interval and a defined progression:

for (i in 36 downTo 2 step 3) println (i) // Display the numbers 36,33, ..., 3

We will come back later in this chapter on the for loop. Also know, as we will see later with the functions, the type void does not exist in Kotlin, but we use instead the type Unit (which does not apply only to functions). Remember that you can see the methods available for a given object in the Kotlin Playground or in the Intellij Idea IDE with the CTRL + ESPACE command after entering the point next to the object. 

Younes Derfoufi 

Leave a Reply