Walking with Kotlin, firsts steps

After Google I/O 2017 Kotlin is ufficially supported by Google, so in these months i have starting to code in Kotlin for work, the language is really cool, many feature are very useful, other strange, but allow you to have a clean code and easy to read.

I don’t like your Null Point Exception

The first thing i have noticed are the nullable variables, Kotlin forces you to valorize them to prevent “The Billion Dollar Mistake”, if a variable is not valorized you can manage it in different ways.

With these syntax is as we put the NonNull annotation and the variable cannot be NULL , in fact the IDE generates and error on it:

to fix it we can add the “?” operator (safe operator), these way is like we put a Nullable annotation, so the variable can be NULL, and we should manage the null point exception inside our code. If you try to invoke a method on this variable you are obliged to use it before method name:

with these example i have introduced another operator “?:” named Elvis Operator, it’s like a ternary operator, if the first condition is null (theResultString is null and you can’t use length method) it use the next value 42.

Another way to manage null variable is the “lateinit” ,in this way we will “promise” to the compiler that the variable will be initialize. Be careful, lateinit is not safe because if we will use that variable before init we generate a specific Kotlin runtime exception. It dosen’t work like a safe operator, it’s the opposite.

Tony Hoare introduced Null references in ALGOL W back in 1965 “simply because it was so easy to implement”, says Mr. Hoare. He talks about that decision considering it “my billion-dollar mistake”.

Class constructors

In Kotlin our classes are very clean, and easy to read, an important contribution is for a new constructors syntax. In Kotlin the constructor can be declared in many way, the first is the constructor in java style:

but it’s not a good practice, with Kotlin we can add a default value to our arguments, so we can rewrite these in these way:

with java we were obliged to create one constructor for specific arguments combination, but…we can do better:

clean and readable: if we need to have one of two argument global inside the class to use it in other function we need only to put var o val before the argument name.

There are other two important thing about the initialization of a class, in Kotlin there are two specific component: init {} and companion {} .
Init is a private class function, every class has an init function and it will execute before the other constructors, for example:

if we try to sum 2+2 the first will be the init result: 0, and the second the constructor result: 4. You can add more init() methods, the execution order is the same of your class, in the example the last line will be “Completed” string.

The companion function it’s only a place to put all static variables or methods like these example:


Class modifier

Kotlin classes are public by default but are final, so they are not extendable, to fix it you should ad the modifier: open

in these example you can see also how pass the super() arguments. The abstract modifier works in the same way of java.


Conclusion

These are first steps in Kotlin, in the next article i will analyze some advance features like: delegates, inline functions, implementation and lambda functions. All feedbacks are welcome!