Introduction to Kotlin (original) (raw)
Last Updated : 04 May, 2025
**Kotlin is a statically typed, general-purpose programming language developed by JetBrains, which has built world-class IDEs like IntelliJ IDEA, PhpStorm, Appcode, etc. It was first introduced by JetBrains in 2011 as a new language for the JVM. Kotlin is an object-oriented language, and a better language than Java, but still fully interoperable with Java code. Kotlin is sponsored by Google, announced as one of the official languages for **Android Development in 2017.
**Example of Kotlin:
Kotlin `
fun main() { println("Hello Geeks"); }
`
**Output:
Hello Geeks
**Key Features of Kotlin
- **Statically typed: Statically typed is a programming language characteristic that means the type of every variable and expression is known at compile-time. Although it is a statically typed language, it does not require you to explicitly specify the type of every variable you declare.
val message= "geeksforgeeks" // refered as a string
- **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 - Kotlin `
/* 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, only one line is used to define the above class -
/* Kotlin Code */
data class Book(var title:String, var author:Author)
- **Concise: It drastically reduces the extra code written in other object-oriented programming languages.
- **Null Safety****:** It provides the safety from most annoying and irritating NullPointerExceptions by supporting nullability as part of its system. Every variable in Kotlin is non-null by default.
String s = "Hello Geeks" // Non-null
If we try to assign "s'' to a null value, then it gives a compile-time error. So,
s = null // Compile-time error
To assign a null value to any string, it should be declared as nullable.
var nullableStr: String = null // compiles successfully
If we try to access the length of the nullableStr variable here.
println(nullableStr?.length) // Compile-time error
We need to perform a null check (?.), before accessing the length function.
println(nullableStr?.length) //null - **Interoperable with Java****:** Kotlin runs on Java Virtual Machine(JVM) so it is totally interoperable with java. We can easily access java code from Kotlin and Kotlin code from Java.
- **Functional and Object Oriented Capabilities: Kotlin has a rich set of many useful methods which including,
- higher-order functions,
- lambda expressions,
- operator overloading
- lazy evaluation and much more.
Higher order function is a function which accepts function as a parameter or returns a function or can do both.
**Example of a higher-order function:
Kotlin `
fun myFun(company: String,product: String, fn: (String,String) -> String): Unit { val result = fn(company,product) println(result) }
fun main(args: Array){ val fn:(String,String)->String={org,portal->"$org develops $portal"} myFun("JetBrains","Kotlin",fn) }
`
**Output:
JetBrains develops Kotlin
- **Smart Cast****:** It explicitly typecasts the immutable values and inserts the value in its safe cast automatically. If we try to access a nullable type of String ( String? = "BYE") without a safe cast, it will generate a compile error.
**With Out Smart Cast: (Compile time error)
fun main(args: Array){
var string: String? = "BYE"
print(string.length) // compile time error
}
}
**With Smart Cast:
fun main(args: Array){
var string: String? = "BYE"
if(string != null) { // smart cast
print(string.length)
}
}
- **Fast Compilation time: It has higher performance and a fast compilation time.
- **Tool-Friendly: It has excellent tooling support. Any of the Java IDEs - IntelliJ IDEA, Eclipse, and Android Studio can be used for Kotlin. We can also be run Kotlin program from command line.
**Advantages of the Kotlin language
- **Easy to learn: Kotlin is almost similar to java, If anybody worked in java then easily understand kotlin in no time.
- **Kotlin is multi-platform: Kotlin is supported by all IDEs of java so you can write your program and execute them on any machine which supports JVM.
- **Safe: It’s much safer than Java.
- **Interoperable: It allows using the Java frameworks and libraries in your new Kotlin projects by using advanced frameworks without any need to change the whole project in Java.
- **Open Source: Kotlin programming language, including the compiler, libraries and all the tooling is completely free and open source and available on github. Here is the link for Github https://github.com/JetBrains/kotlin.
**Applications of Kotlin language
- You can use Kotlin to build an Android Application.
- Kotlin can also compile to JavaScript, making it available for the frontend.
- It is also designed to work well for web development and server-side development.
- Cross-Platform with Kotin Multiplatform.