Hello there, my name is Divyansh Jain student of Mahatma Jyoti Rao Phoole University. I’ve been selected as the Android developer for the XWiki organization. And I’m working on their XWiki Android Authenticator application in GSOC19.
XWiki android Authenticator aims to integrate a wiki instance in Android accounts, mainly including the synchronization of contacts and the XWiki authenticator. By synchronizing contacts of your company on your phone, it's easy to communicate and collaborate with each other.
Since the starting of my work in XWiki Android Authenticator, I’ve been constantly learning new things. In the first week, I’ve migrated most of the XWiki Android Application code from Java to Kotlin. And on this page, I would like to share my understanding of the kotlin.
This page is for:
- Anyone who is new to kotlin.
=== Getting started with Kotlin ===
Kotlin is officially supported by Google for mobile development on Android. It was released in Android Studio 3.0 on October 2017. At first, I was a bit skeptical to switch to Kotlin, It’s a new language, but I started learning that it has quite a few advantages such as:
1. Java Interoperability: I started with migrating the whole XWiki Android Authenticator app code from Java to kotlin. I was replacing one Java file at a time, and while migrating I see that kotlin works with Java smoothly, though it requires some direct imports other than that when I ran the app in the device it works just fine, there was no error and the app was working fine.
1. Familiar syntax: Kotlin literally has familiar syntax compared to Java, which is why I learned the kotlin in just a matter of days. Though there is some difference like.
11. Changed variable declaration: In Java, we used to declare variables. E.g.
String wiki, now it’s val str = “Hello” or val str: String = “Hello”.
Here val declares a read-only property or local variable whereas var declares a mutable property or local variable.
11. The final keyword is default in class: In kotlin final is a default. E.g.
class Button {
fun click() = print("Click")
}
class displayToast : Button() { // Error
override fun click() = print("Toast Displayed") // Error
}
In the above example, class displayToast can’t inherit Button class because it is final. Moreover, it can’t override click(), because it is final in Button. In order to inherit and override we put “open” keyword which allows inheritance and overriding. E.g.
open class Button {
open fun click() = print("Click")
fun doubleClick() = print("Double Click")
}
class displayToast () : Button { // Inheritance is now possible
override fun click() = print("Toast Displayed") // Now it works
override fun displayToast () = print("Toast Displayed") // Error
}
11. Fun keyword for defining functions: Now in kotlin there’s a new way to define functions. E.g.
fun displayToast() {
}
or
fun addDigitis (a: int, b: int) : String {
}
11. The when expression: The switch case is replaced with the much more readable and flexible when expression: e.g.
int x = 3;
when (x) {
1 -> print("x is 1")
2 -> print("x is 2")
3, 4 -> print("x is 3 or 4")
in 5..10 -> print("x is 5, 6, 7, 8, 9, or 10")
else -> print("x is out of range")
}
It works without the argument too.
11. Static keywords : For declaring static methods & variables, you can put them above the class name, then you can use them by importing directly in other classes.
1. Null Safety: One of the biggest flaws in Java is the way it handles “null,” leading to the dreaded NulPointerException (NPE). Kotlin resolves this by distinguishing between non-null types and nullable types. Types are non-null by default, and can be made nullable by adding a safe call ‘?’. E.g.
var a: String = "abc"**
a = null // compile error
var b: String? = "xyz"
b = null // no problem