Google has recently announced that it is going to remove all sort of “expected ‘;’” errors from Android Studio in their next major update for Android Studio, in version 3. The previous sentence was a total pun, and it was actually the way I first interpreted their shift from Java code base to Kotlin. I don’t get the point, why is everyone hating the semicolon so much. Everyone keeps introducing new languages, and the most notable feature that distinguishes from the older languages is the semi-colon removal.
Enough gossip, now let’s talk somewhat reality into the post and have a look at what we have right now, which has caused somewhat tremendous amount of havoc on the internet, after those famous cat videos of course.
Is Kotlin New to Android?
Android now officially supports Kotlin programming language, along with Java and the native programming using C++ language.
Figure 1: Kotlin logo.
A typical question would be: Is this a new programming language to learn? Definitely no, and it is definitely not a new language for Android as well. Kotlin had been in the game since a while and if you have not yet heard about it, means, that you did not surf around in the Android world enough. Kotlin has been around for quite a long time and mostly, Android developers have already started using Kotlin language as their default language over Java. To me, the entire concept came as a new thing and at the first look, I was like, what the heck is this? Am I going to learn all of this now?
But, as the time passed, and I was to convert and see the difference of the languages; Java and Kotlin, I came to realize they shared most of the concepts and syntax, however, some of the stuff resembles Swift, and some C++ (or even C#) and some of the concepts were taken from “defensive programming”, and so on and so forth. Although the language was impressive, the news that came on the Internet was not - as I did not watch the Google I/O 2017 so I did not know what happened, until I did watch it. I came to easily realize that the Kotlin language was not a new one, or that Google did not say that they are going to leave Java out and everyone would be required to program in Kotlin, they are both (Java and Kotlin, even C++) are all interoperable. Your current projects would continue to be in Java, until or unless you want to migrate the code base from one language to another one; new one. This provides the developers with a flexibility to migrate the code base, or work on the existing one: Your Android dev team can be split up into other sections, C++, Java and now Kotlin as well. Pretty interesting right (hehe, can’t imaging I said this…).
Figure 2: Android promoting Kotlin, or vice versa.
So, the benefits are really much amazing and counting, there would be much more by Google that you can wait for, to get and enjoy in the near future.
How to Get Kotlin for Android
In my own opinion, it was a great step by Google to actually integrate the Kotlin language in Android Studio. Why? For several reasons:
- Android developers were already using Kotlin language for their projects.
- There was a plug in previously, that they needed to install to actually migrate the code base from Java to Kotlin. Also required some of the tweaking to the current build systems; to support Kotlin.
- IntelliJ based system, and IntelliJ based language. Why not, eh?
Thus, Google announced that they will start shipping Kotlin support, built in. Android Studio 3 (which is, at the time of writing the post, a Canary version), would be the first of the versions to actually get the Kotlin packages pre installed and a complete support to develop Android applications in Kotlin language.
There are several other blog posts that you should definitely consider reading if you are new to the entire Kotlin concept.
References:
- Kotlin on Android
- Get Started with Kotlin
Improvements that Kotlin Brings
Someone has to say that this is a better language as compared to the older one, right? Sadly, I am not that someone. Too much of syntax simplicity, makes me “think”! If ignoring the common improvements that Kotlin brings, and having a look at what Android Studio has to offer, we can summarize this by saying:
“Android Studio speaks Kotlin”.
The most notable feature, as of Android Studio 3 is that now you can simply code your Java code from a Java class, and try to copy it in a Kotlin file, Android Studio will automatically convert that code from Java to Kotlin and print it there for you!
Figure 3: Dialog from Android Studio confirming to convert Java code to Kotlin.
I also got a chance to actually carefully study the current benchmarking, and some other references by other experts on Kotlin and/or the people who have had been working in the Kotlin environment, and I was to learn that you can actually love Kotlin for various reasons, and while being developed on top of (or beside) Java, there are several ways in which Kotlin can be useful:
- It has a better syntax, and a worse syntax in most cases.
- Shorter and concise syntax, functions are an example of this, but uglier use of generics, lambdas and the inheritance is shameful. Most of the features were “stolen” from various languages, and shamefully shown to be working.
- Of course, if the code compiles, it means, the language succeeded! Hello world.
- Acts as a wrapper around the JVM, so you can expect that it will not let you roam around the sensitive areas - areas where an exception will result in app not responding or app crashes.
- Kotlin doesn’t bring much improvements as performance counts, the reason being that the languages both compile down to the bytecode and that bytecode actually is what executes. A good program, with a better logic would run faster, but the language (Kotlin or Java) doesn’t matter much here.
- On the other hand, Kotlin itself, brings around 7000 of extra methods to the overall API and somewhat an extra ~1MB of the APK file size but Google suggests that you can minimize it, by the use of ProGuard or other services.
- The compilation time is still almost the same, sometimes Java has a benefit of around 17%, sometimes Kotlin wins and it all depends on what you’re doing, not what the language has; besides, they both boil down to JVM bytecode.
- Language improvements are the most notable feature; the first thing that you notice in this is the size of the programs that you will write. The lines of codes are really less.
But the thing is, your life as a developer would be much more simpler, neater and cleaner once you get your hands dirty in the programming fest with Kotlin as compared to Java.
Finally, there were a few bad things that I saw in Kotlin, some of us are living a happy life provided the existence of the “static
” modifier, Kotlin doesn’t support that. Please review the references URLs and see “The Bad” section to know more on this.
References:
- Kotlin vs Java: Compilation Speed
- Kotlin: The Good, The Bad, and The Ugly
Want to Learn Kotlin?
If you are a beginner to Kotlin, then you would definitely be wanting to learn about the Kotlin language, fortunately, there is a tutorial and an online compiler that you can use. The online Kotlin resources provide a basic Hello World program that you can use for your own main learning goals. You should consider trying it out.
Secondly, there is also a feature provided where you can convert your Java code and learn what sort of Kotlin code gets generated to understand the typical Java ↔ Kotlin interoperability for the language tokens.
For example, I was using the online terminal and I convert the following code from Java to Kotlin:
public class Person {
private int age;
private String name;
public int getAge() { return this.age; }
public void setAge(int age) { this.age = age; }
public String getName() { return this.name; }
public void setName(String name) { this.name = name; }
public void sayHello() {
System.out.println("Hello, my name is " + getName());
}
public int older(int years) {
return getAge() + years;
}
private void breathe() {
System.out.println("I am breathing...");
}
}
The Kotlin code was like the following code:
class Person {
var age:Int = 0
var name:String
fun sayHello() {
println("Hello, my name is " + name)
}
fun older(years:Int):Int {
return age + years
}
private fun breathe() {
println("I am breathing...")
}
}
The screenshot of the web interface that provided this feature is as follows:
Figure 4: A GUI to convert Java code to Kotlin.
Provided that you forget about the semicolons, and a few other extra things, you will notice there are still a lot of changes to the language and some are interesting, some are unfathomable. Even by this very simple example of code, you can easily realize that the Kotlin language:
- Supports classes, and they are
public
(see the Java alternative code). - It supports the properties
- Much more like C#, it does not have that getter setter style of encapsulating the
private
fields in public
getters and setters.
- The functions start with “
fun
”, but then become painful. - Rest of the code is similar, you write the print line function, you can perform operations on the language’s primitive types (integers being added here).
- You must provide the
private
modifier, public
is default - or am I missing something?
I also have to figure out a few of the things here, most of the things are handy, and easy to understand, but majority of the stuff is still confusing for me as to why I should use them, or why to stick to the stuff I already have.
So, do try it out, and also if you want to learn the bleeding edge technology, consider using Android Studio 3 and start building Android applications using Kotlin language, out of the box.