Top 1,000 Features Creators Events Podcasts Extensions Blog Explorer CSV Download

Kotlin

< >

Kotlin is an open source programming language created in 2011 by Andrey Breslav.

#26on PLDB 13Years Old 901kRepos
Download source code:
git clone https://github.com/JetBrains/kotlin

Try now: Riju ยท TIO ยท Replit

Kotlin is a statically-typed programming language that runs on the Java virtual machine and also can be compiled to JavaScript source code or use the LLVM compiler infrastructure. Its primary development is from a team of JetBrains programmers based in Saint Petersburg, Russia. While the syntax is not compatible with Java, Kotlin is designed to interoperate with Java code and is reliant on Java code from the existing Java Class Library, such as the collections framework. Read more on Wikipedia...


Example from Compiler Explorer:
// Type your code here, or load an example. fun square(num: Int): Int = num * num
Example from Riju:
println("Hello, world!")
Example from hello-world:
fun main() { println("Hello World") }
// Hello world in Kotlin fun main(args : Array<String>) { println("Hello, world!") }
Example from Linguist:
package addressbook class Contact( val name : String, val emails : List<EmailAddress>, val addresses : List<PostalAddress>, val phonenums : List<PhoneNumber> ) class EmailAddress( val user : String, val host : String ) class PostalAddress( val streetAddress : String, val city : String, val zip : String, val state : USState?, val country : Country ) { assert {(state == null) xor (country == Countries["US"]) } } class PhoneNumber( val country : Country, val areaCode : Int, val number : Long ) object Countries { fun get(id : CountryID) : Country = countryTable[id] private var table : Map<String, Country>? = null private val countryTable : Map<String, Country> get() { if (table == null) { table = HashMap() for (line in TextFile("countries.txt").lines(stripWhiteSpace = true)) { table[line] = Country(line) } } return table } } class Country(val name : String)
Example from Wikipedia:
// returns null if... // - foo() returns null, // - or if foo() is non-null, but bar() returns null, // - or if foo() and bar() are non-null, but baz() returns null. // vice versa, return value is non-null if and only if foo(), bar() and baz() are non-null foo()?.bar()?.baz()
Kotlin Keywords
as as? break class continue do else false for fun if in !in interface is !is null object package return super this throw true try typealias val var when while by catch constructor delegate dynamic field file finally get import init param property receiver set setparam where actual abstract annotation companion const crossinline data enum expect external final infix inline inner internal lateinit noinline open operator out override private protected public reified sealed suspend tailrec vararg field it + - * / % * = += -= *= /= %= ++ -- && || ! == != === !== < > <= >= [ ] !! ?. ?: :: .. : ? ->; @ ; $ _

Language features

Feature Supported Token Example
Conditionals โœ“
Access Modifiers โœ“
Functions โœ“
Exceptions โœ“
Constants โœ“
Classes โœ“
While Loops โœ“
Booleans โœ“ true false
Strings โœ“ "
MultiLine Comments โœ“ /* */
/* A comment
*/
Print() Debugging โœ“ println
Line Comments โœ“ //
// A comment
Type Inference โœ“
Operator Overloading โœ“
Interfaces โœ“
interface Named {
   val name: String
}
interface Person : Named {
    val firstName: String
    val lastName: String
    
    override val name: String get() = "$firstName $lastName"
}
File Imports โœ“
import foo.Bar // Bar is now accessible without qualification
import foo.* // everything in 'foo' becomes accessible
import bar.Bar as bBar // bBar stands for 'bar.Bar'
Ternary operators โœ“
// Since if statements are expressions
println(if (true) true else false)
Comments โœ“
Assignment โœ“
table = HashMap()
Case Insensitive Identifiers X
Semantic Indentation X
Macros X
Variable Substitution Syntax X

View source

- Build the next great programming language ยท About ยท Resources ยท Acknowledgements ยท Part of the World Wide Scroll