How to make the life of a Java developer easier?
This article talks about the programming language Kotlin. You will learn about the reasons for the appearance of the project, it's help of technologies, the possibilities of the language, and see a few examples.
The article is written primarily in the expectation that the reader is familiar with the programming language Java, however, knowing another language, too, can give an idea about the subject.
Why is this language better than Java?
Well, how many people have attempted to write code for Android in PHP / C / C ++ / C # / or Python, without success? Perhaps this time is different, and it is worth paying attention to?This language was developed by the company ‘JetBrains’, and already is quite established. Their desire to create their own language for Android development without Java continues and it is commendable deserving respect. Moreover, the main goal for themselves is to completely translate the development within the company from Java to Kotlin, and they want it, like Java compiler for Android phone, to be used within the industry. However, some call it "Swift for Android"! And not just people from the street, but the developers themselves Kotlin! They say that it's possible to forget about NPE in runtime; that it is possible to extend standard classes from the SDK, that lambdas and anonymous functions can be written and even passed functions as parameters to methods! Even a semicolon was removed from the syntax!
Deep into details
It's interesting that Kotlin is 100% compatible with Java, and the plug-in allows java-code to be transformed into one kotlin-code in one click. It turns out that because of compatibility with Java, the new TestActivity on Kotlin is quietly inherited from AppCompatActivity, which lies in the Support Library, not in the SDK. Well, it's quite easy to move to the Kotlin rails and make the use of Java libraries. Of course, this is a plus, perhaps, it's worth writing a point in favor of Kotlin.
One line of code compared to the 14 in the Java version! And if you add data before the class description, then we get the ability to make copies of objects using the copy () method. As parameters, it can accept new values of the object fields.
Running under the "Classes and Objects" section of the documentation, you will learn that:
- To create a class instance, we do not need the keyword new
- No extends or implements to you, now ":" are used for this
- A class can have one primary constructor and one / several secondary constructors
- Primary constructor - does not have a body, but inside it you can describe the init initialization block
- The keywords var (mutable property) / val (read-only property) are used for declaration and initialization of properties directly in the declaration of the constructor. The keywords val \ var are also used when declaring ordinary local variables
- There are named arguments and default values for them
- You can describe custom accessors
- Now you do not need to explicitly call getter / setter to get / assign the value of the property - the call is just by name!
- By default, the scope for classes / methods / properties is public
- By default all classes in Kotlin are final and you need to use the open-annotation for the class, if you need a different behavior
- Interfaces can now implement the methods! Those A class can implement two interfaces that have a different implementation of the method with the same signature, and you can choose where to use which method;
- The variability of generics is realized with the help of in / out keywords
- Implement anonymous inner classes using object expressions & object declarations
- All favorite Singleton are easily implemented using object declaration
- Java static methods == object declaration + companion object
- Delegation is implemented at the declaration level of the class with the help of by-clause, the compiler itself generates the necessary duplicate methods
We should also note that the features that affect the method signature, also apply to the usual functions.
You can also take a java-code and copy it into a kotlin-file and the IDE will offer to convert it. By the way, on Java it is possible to write code in kotlin-files, and all will work is as one more bonus of 100% compatibility. In the same place with the help of IDE the received code can be simplified and lead to kotlin-style. This will help to speed up learning and shift thinking to Functional Programming (FP).
If, after all of the above, you are seriously thinking about moving to Kotlin, here's a fly in the ointment. Unfortunately, it is still difficult to find good specialists who can easily cope exclusively with this language. Therefore, if we are talking about development in certain industries, we recommend:
- For fintech - Java, since it would be hard to find a skilled back-end developer in Kotlin.
- For IOT - Kotlin mobile UI framework. According to experts, the project price is automatically reduced by 20%.
- For ecommerce - here is up to you. But as we mentioned earlier, Kotlin will save your time.
Plans for the future
As TechCrunch writes, Google was told about the organization together with the creators of the language - JetBrains - a special fund for the development of Kotlin.
In Google, stressed that Kotlin will be an additional language and in no case will replace Java and C ++ (to date, the language of the first class for Android is Java). It is planned that tools for Kotlin, based on the JetBrains IDE, will be standard and included in Android Studio 3.0 - the official development tool for the Android OS.
"Since Kotlin is fully supported by Java, you could write applications on it earlier, but now it will be supported by the authority and reputation of Google," the journalists note.
Your Ardas Team