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 –
static int num1, num2;
num1 = 20 ;
num2 = 30 ;
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
fun getName() : String? = null
val name: String? = null
val len = name.length
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 – 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:
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.