Kotlin - The official language for android development.
Kotlin
Kotlin is a statically-typed programming language that runs on the Java Virtual Machine and also can be compiled to JavaScript source code. Its primary development is from a team of JetBrains programmers based in Saint Petersburg, Russia (the name comes from Kotlin Island, near St. Petersburg).[3] While not syntax compatible with Java, Kotlin is designed to interoperate with Java code and is reliant on Java code from the existing Java Class Library, such as the collections framework. Kotlin is similar to Apple's Swift.
History
In July 2011 JetBrains unveiled Project Kotlin, a new language for the JVM, which had been under development for a year.[5]JetBrains lead Dmitry Jemerov said that most languages did not have the features they were looking for, with the exception of Scala. However, he cited the slow compile time of Scala as an obvious deficiency.[5] One of the stated goals of Kotlin is to compile as quickly as Java. In February 2012, JetBrains open sourced the project under the Apache 2 license.[6]
JetBrains hopes that the new language will drive IntelliJ IDEA sales.[7]
Kotlin v1.0 was released on February 15, 2016.[8] This is considered to be the first officially stable release and JetBrains has committed to long-term backwards compatibility starting with this version.
In Google I/O 2017, Google announced first-class support for Kotlin on Android.[9]
Converting Java code to Kotlin:
Open
MainActivity.java
file. Then invoke action Convert Java File to Kotlin File. You can do it by several ways. The easiest one is to invoke Find Action and start typing an action name (like in a screencast below). Alternatively you can call this option via the Code | Convert Java File to Kotlin File menu entry or by using the corresponding shortcut (you can find it at the menu entry).
After the conversion you should have an activity written in Kotlin.
Features:
Kotlin stands out in a sea of new programming languages because of its focus on the ecosystem: JetBrains understand that productivity comes from more than convenient syntax.
Despite that, Kotlin has many useful features that make writing code in it pleasant:
- We already mentioned null safety (optionality), that lets the compiler systematically flag potential null pointer dereferences. Unlike some languages, this does not involve an option type and is therefore zero-overhead. Other language features ensure it’s not inconvenient.
- Lean syntax: type inference works everywhere, one liner functions take one line, simple structs/JavaBeans can also be declared in one line. Real properties generate getFoo/setFoo methods behind the scenes for Java interop. Functions can exist outside of classes.
- Exceptions are unchecked.
- Adding the data annotation to a class triggers autogeneration of boilerplate like equals, hashCode, toString, a copy method and variable spreading support (destructuring). This gives you convenient immutable classes without the need for builders.
- But if you do need to construct complex structures, a clever intersection of language features makes builders clean and type safe (read: auto-completable). If you use Google Protocol Buffers to store structured data, that gets easier too.
- Functional programming support with zero-overhead lambdas and ability to do mapping, folding etc over standard Java collections. The Kotlin type system distinguishes between mutable and immutable views over collections.
- Extension functions let you add methods to classes without modifying their source code. This looks at first like a superficial bit of syntax sugar to avoid FooUtils style classes. Then you realise that doing it this way enables you to easily discover the new methods via auto-completion, lets you build powerful language extensions and lets you integrate existing Java APIs with other Kotlin features. Features like …
- Operator overloading. But the good kind: no Scala / Perl style line noise here. Operators map to special method names, so can override the behaviour of the existing operators (including function invocation), but you cannot define entirely new ones. This strikes a balance between power and readability.
- Kotlin does not have macros or other ways to redefine the language, but a collection of carefully designed features allow for libraries that act like language extensions far more than they act like collections of objects. A good example: would you like to use fibers, actors, and Go-style channels? A library called Quasar has you covered.
- Markdown instead of HTML for your API docs. This makes writing JavaDocs much more pleasant. The “Dokka” tool, which is the equivalent of JavaDoc, can read both Kotlin and Java source code and generate combined doc websites, both with its own style and also in the standard JavaDoc HTML style.
- Better generics. If you never fully got to grips with what exactly super and extends mean when put inside a type variable, don’t worry: it’s not you. Java’s generics really are just confusing. Kotlin fixes it.
- Delegation (forwarding methods) can be done automatically.
- The == operator does what you actually expect.
- Would you like fast and convenient async programming? Of course you would.
- String interpolation “works like ${this.example}!”
- Function arguments can be named, optional and variadic.
- Many, many other tweaks and improvements. If something annoyed you about Java, I give it 50/50 that it’s fixed in Kotlin.
Comments
Post a Comment