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

val message= "geeksforgeeks" // refered as a string

/* 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)

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

**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)
}
}

**Advantages of the Kotlin language

**Applications of Kotlin language