Kotlin vs Java
“Kotlin is a new programming language that will force you to abandon Java.” At the Zebra APPFORUM 2017 European conference in Prague, our Android developer Peter Otten inspired others to start writing on Kotlin. Upset for missing? Do not worry! Peter will tell you why he became a big fan of this language.
Kotlin is programming languagedeveloped by the company Jetbrainswhich is famous for IntelliJ, WebStorm, PhpStorm and the ReSharper plugin for Visual Studio. They went in search of a new programming language that would be better than Java, but still functionally compatible with it. Inspired by the Java, C #, Scala, and Groovy languages, the Jetbrains folks put together a team to develop a new programming language. Kotlin was designed by people who experienced all the pain of Java.
So what is Kotlin?
Compared to Java
For comparison, Java and Kotlin at the presentation, the POJO class and how it can be used were shown as an example (Figure above). Here you can see all the power and conciseness of Kotlin when a simple class Person (with name, getter / setter and standard POJO methods) is replaced by the keyword “Data”. Also, looking at the use of the Person class, you can notice the following differences:
- Methods in Kotlin are called
fun(this immediately makes programming more fun);
- If the method does not return anything, then you do not need to specify the type of the return value (in Java, write “Void”);
- Kotlin has type interference, so indicate that “name” is of type “String” not necessary. Simple use “Val”.
- This feature also makes the variable immutable. If you want to change the variable “Name“need to use “Var”. So Kotlin makes you think ahead of time when writing code about variables / fields, etc.
- To create a new instance of the class, the keyword “New” not necessary;
- Kotlin and Java are compatible. Kotlin can use the class without any problems. Personwritten in Java.
- Getters and setters from Java code will automatically become properties, i.e. “GetName ()” will not be available in Kotlin, but you can use the property “Name” from Person.
Mediaan Conclusions on Using Kotlin
After visiting other Kotlin reports on Droidcon in 2015 and 2016 in London and GDG DevFest 2016 in Amsterdam, our mobile development team decided it was time to take a look at the new language. We started using it in October 2016 and just fell in love with it. The first new Android project was already 100% written in Kotlin. Since then, we have not returned to Android development in Java.
Now, when we look back, seeing our experience with Java, and see how Kotlin is used now, we can draw the following conclusions:
- The code is more concise, that is, you can write and maintain less code;
- The code is more secure since the language makes you think of (un) mutability, nullability, etc. while writing code;
- The above also accelerates development. It becomes easier for you to do complex things.
- Excellent IDE support (we use Android Studio);
- A large and growing community. Existing libraries are supported or migrated to Kotlin, the Internet is full of blog posts, videos, presentations and, of course, questions / answers to
- Kotlin is ready for production. We use it in production as well as many companies such as Square, Pinterest, Trello and Google.
Now Google has announced that Kotlin is the official development language for Android, and you have virtually no reason not to look at this language and start using it.
So, will you start with Kotlin or go to it?
There are many links that will help you in its development:
- Official site;
- Documentation (manuals, guides, books, etc.);
- Online programming;
- Android development;
- Speeches, blogs, and presentations on YouTube, Medium, and SlideShare.
In addition, the plug-in for Android Studio has the ability to quickly convert your current open Java file to Kotlin. It works pretty well. You may need to tweak some little things in the code, but you will instantly get a Kotlin version of your Java code for comparison and study. There is another amazing possibility: when you work in a file in Kotlin and paste a piece of Java code into it, it is automatically converted to Kotlin code when you insert it.
To use Kotlin in an existing project or to completely transfer an existing project to it, we recommend the following approach:
- Use interoperability (Kotlin and Java can work side by side);
- Use the plugin to convert files and code snippets.
- Write all new features on Kotlin;
- Everything you edit, convert from Java to Kotlin.