Kotlin | Language for Android, now Official by Google
Kotlin is a new Open-Source programming language from JetBrains. It first appeared in 2011 when JetBrains unveiled their project named “Kotlin”. Basically like Java, C and C++ — Kotlin is also a “statically typed programming language” (languages in which variables need not be defined before they are used).
Static typing does not mean that we have to declare all the variables first before we use them. Variables may be initialized anywhere in the program when there is a need.
Consider the following example –
/* Java Code */ static int num1, num2; //explicit declaration num1 = 20 ; //use the variables anywhere num2 = 30 ; /* Kotlin Code*/ val a: Int val b: Int a = 5 b = 10 |
Kotlin support is a chance to use a modern and powerful language, solving common headaches such as runtime exceptions and source code verbosity. Kotlin is easy to get started with and can be gradually introduced into existing projects which means that your existing skills and technology investments are preserved.
Features of Kotlin Language:
- Kotlin avoids the Null Pointer Exception i.e., If we try to assign or return null to a variable or function respectively, then it won’t compile. To compile it we have to add “?” after the variable type.
Consider the following exampleval name: String? =
null
//assigned null and it will compile also.
fun getName() : String? =
null
//returned null and it will compile too.
/* won’t compile */
val name: String? =
null
val len = name.length
/* correct way */
val name: String? =
null
val len = name?.length
- Versatile
Comparison with Java
- Null Safety– As already mentioned that Kotlin avoids Null Pointer Exception i.e., it fails at compile-time whenever a Null Pointer Exception occurs.
- Data Classes– In Kotlin, there are Data Classes which lead to auto generation of boilerplate like equals, hashCode, toString, getters/setters and much more.
Consider the following example –/* Java Code */
class
Book {
private
String title;
private
Author author;
public
String getTitle() {
return
title;
}
public
void
setTitle(String title) {
this
.title = title;
}
public
Author getAuthor() {
return
author;
}
public
void
setAuthor(Author author) {
this
.author = author;
}
}
But in Kotlin the above same class can define concisely in one line –
/* Kotlin Code */ data class Book(var title:String,var author:Author)
- Type Inference– In Kotlin, there is a great thing that you don’t have to specify the type of each variable explicitly(in clear and detailed manner). But if you want to define a data type explicitly, you can also do that.
Consider the following example –/* not explicitly defined */ fun main(args: Array) { val text = 10 println(text) } /* explicitly defined */ fun main(args: Array) { val text: Int = 10 println(text) }
- Functional Programming– Kotlin consist of many useful methods which includes higher-order functions, lambda expressions, operator overloading, lazy evaluation, operator overloading and much more.
Functional Programming makes Kotlin much more handier when it comes to collections –fun main(args: Array) { val numbers = arrayListOf(15, -5, 11, -39) val nonNegativeNumbers = numbers.filter { it >= 0 } println(nonNegativeNumbers) }
Output:
15,11
-
Smart Casts– When it comes to casts, Kotlin compiler is really intelligent. In many cases, one does not need to use explicit cast operators, but in Kotlin there is “is-checks” for immutable values and inserts casts automatically when needed-
fun demo(x:Any){ if(x is String){ print(x.length) // x is automatically cast to string } }
Benefits of Kotlin Language:
- Kotlin compiles to JVM bytecode or JavaScript-Like Java. Bytecode means Programming code that once compiled is run through a virtual machine instead of the computer’s processor. By using this approach, source code can be run on any platform once it has been compiled and run through the virtual machine. Once a kotlin program has been converted to bytecode, it can be transferred across a network and executed by JVM(Java Virtual Machine).
- Kotlin programs can use all existing Java Frameworks and Libraries.
- Kotlin can be learned easily and it is approachable. It can be learned easily by simply reading the language reference. The syntax is clean and easy to use and understand.
- Kotlin is Open Source and it costs nothing to adopt.
- Kotlin’s null-safety is great. This type of system helps us to avoid null pointer exceptions by refusing to compile code that tries to assign or return null.
val name: String = null // tries to assign null, won’t compile. fun getName() : String = null // tries to return null, won’t compile.
- Kotlin much more focuses on readable syntax so code reviews are not a problem.
Some Facts about Kotlin:
- The currently released version is 1.1.3-2 published on July 4, 2017.
- Kotlin is free, has been free and will remain free. It is developed under the Apache 2.0 license and the source code is available on GitHub.
- Kotlin is more concise when compared to Java and is 100% interoperable with it.
- Kotlin is supported as First-class language on Android though it can be used for any kind of development, be it server-side and client-side web.
- Kotlin is supported by all major Java IDEs including IntelliJ IDEA, Android Studio, Eclipse and NetBeans.
Some useful links:
This article is contributed by Aakash Pal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Please Login to comment...