Top 1K Features Creators Events Podcasts Books Extensions Interviews Blog Explorer CSV

Kotlin

< >

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

#25on PLDB 13Years Old 901kRepos
Download source code:
git clone https://github.com/JetBrains/kotlin
Homepage · Leet Sheet · REPL · Try It Online · Source Code · Wikipedia · Subreddit · Twitter · FAQ · Release Notes · Docs

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 Example Token
Standard Library println("Hello, World!")
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

- Build the next great programming language · Add · Issues · About · Search · Keywords · Livestreams · Labs · Resources · Acknowledgements

Built with Scroll v154.3.0