Questions Concepts
GitHub icon

Kotlin

Kotlin - Programming language

< >

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

Source code:
git clone https://github.com/JetBrains/kotlin
#26on PLDB 12Years Old 901kRepos

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 · Search · Add Language · Features · Creators · Resources · About · Blog · Acknowledgements · Queries · Stats · Sponsor · Traffic · Traffic Today · Day 305 · feedback@pldb.com · Logout