Introduction
Ever since Apple released the Swift programming language for iOS, lots of Android developers have wanted a similar language for Android development. If you are one of those developers, you are going to love Kotlin, a JVM language that is remarkably similar to Swift.
Kotlin is a statically-typed language, developed by JetBrains, whose syntax is more expressive and concise than that of Java. With features like higher-order functions, lambda expressions, operator overloading, string templates, and more, Kotlin has a lot more to offer than Java. Because Java and Kotlin are highly interoperable, they can be used together in the same project.
If you are a competent programmer in Java, you will be able to learn Kotlin in a very short time. In this tutorial I will be showing you how to use Kotlin’s most frequently used constructs.
Basics
- You do not need
;
to break statements. - Comments are similar to Java or C#,
/* This is comment */
for multi line comments and// for single line comment
. - Unlike Java, you do not need to match your file name to your class name.
- Like JavaScript, you can create functions outside classes. So there is no need to stuff your functions as static members of classes like what you do in C# or Java.
- Kotlin has string templates, which is awesome. e.g.
"$firstName $lastName"
for simple variable name or"${person.name} is ${1 * 2}"
for any expressions. You can still do the string concatenation if you like e.g."hello " + "world"
, but that means being stupid. - It has no tuple although Kotlin's data classes is an option to use in place of tuple.
Features of Kotlin programming language
1. Kotlin is open source
First and foremost, Kotlin in an open source programming language. This statically typed language was built by Jetbrains, who are also makers of IntelliJ IDE. Apart from being an open source programming language, Kotlin doesn’t ask much when it comes to converting existing Java code, it happens with a single-click tool. But, if you wish to learn Kotlin from the scratch, there’s great and cost-effective
2. Full Java Interoperability
One of the best features of Kotlin programming language is its deep interoperability with Java, which is bound to attract more Java developers to learn Kotlin. It runs on the JVM and uses Java libraries and tools. It offers backward compatibility for Java versions 6 and 7.
3. Kotlin compiles to JVM bytecode or JS
As Kotlin compiles to JVM bytecode or JavaScript, Java and JS developers will be most tempted to learn this programming language. Added to this, the programmers who use a garbage collected runtime will also find Kotlin programming language interesting.
4. Data Classes in Kotlin
The feature scope and necessity of a class is always argued by the programming language designers. A typical data class in Java has tons of boilerplate code one needs to skip while finding out the real use of that class. However, in Kotlin, you can write the equivalent of the same Java code in a very simple manner and save tons of typing and mental effort. It’s one of the best features of Kotin programming language.
5. Defaulted parameters
The defaulted parameters in Kotlin are pretty handy when you pass the arguments by name, instead of index. Their advantage is seen when there’s a function with tons of optional parameters.
6. Kotlin imposes no runtime overhead
The standard Kotlin library doesn’t have garbage, it’s tight and small. It has mostly focused extensions to the Java standard library. Many of its functions are inline-only that just become inline code. Kotlin has many optimizations which, specifically, help Android development.
7. Null Safety in Kotlin
Kotlin programming language’s type system aims at eliminating the perils of null references from code, which is often called The Billion Dollar Mistake. For example, in Java, accessing a member of null reference results in a null reference exception. Kotlin doesn’t compile code that assigns or returns a null. This is seen as one of the most important features of Kotlin.
8. Extension functions
Thanks to the extension functions in Kotlin, you can add methods to classes without making changes to their source code. Similar to Scala’s implicit methods, you can add methods on a per-user basis to classes. While extension functions are often controversial, every now and then it’s very useful.
9. Swift is like Kotlin
It’s a feature of Kotlin, but I came across this great link on Hacker News, so I’ll just leave it here to do all the talking: http://nilhcem.com/swift-is-like-kotlin/
10. Kotlin wants you to write less code
Unlike Java, which needs you to write everything, Kotlin compiler can understand from the code and write the remaining code, for example, it can infer types in variable declarations. This increases productivity and saves time.
Kotlin makes Android development more fun
If you search the web, you’ll find tons of ways in which Java’s pain points are solved by Kotlin and how features of Kotlin aim to make Android development more fun. You can use it write more expressive and effective code with fewer bugs.