page = kotlin programming language
url = https://kotlinlang.org
Simple
Asynchronous
Object-oriented
Functional
Ideal for tests
Run
val name = "stranger" // Declare your first variable
println ( "Hi, $name!" ) // ...and use it!
print ( "Current count:" )
for ( i in 0 .. 10 ) { // Loop over a range from 0 to 10
print ( " $i" )
Target: JVM Running on v.1.6.0
import kotlinx . coroutines . *
suspend fun main () { // A function that can be suspended and resumed later
val start = System . currentTimeMillis ()
coroutineScope { // Create a scope for starting coroutines
for ( i in 1 .. 10 ) {
launch { // Start 10 concurrent tasks
delay ( 3000L - i * 300 ) // Pause their execution
log ( start , "Countdown: $i" )
// Execution continues when all coroutines in the scope have finished
log ( start , "Liftoff!" )
fun log ( start : Long , msg : String ) {
println ( "$msg " +
"(on ${Thread.currentThread().name}) " +
"after ${(System.currentTimeMillis() - start)/1000F}s" )
abstract class Person ( val name : String ) {
abstract fun greet ()
interface FoodConsumer {
fun eat ()
fun pay ( amount : Int ) = println ( "Delicious! Here's $amount bucks!" )
class RestaurantCustomer ( name : String , val dish : String ) : Person ( name ), FoodConsumer {
fun order () = println ( "$dish, please!" )
override fun eat () = println ( "*Eats $dish*" )
override fun greet () = println ( "It's me, $name." )
val sam = RestaurantCustomer ( "Sam" , "Mixed salad" )
sam . greet () // An implementation of an abstract function
sam . order () // A member function
sam . eat () // An implementation of an interface function
sam . pay ( 10 ) // A default implementation in an interface
// Who sent the most messages?
val frequentSender = messages
. groupBy ( Message :: sender )
. maxByOrNull { ( _ , messages ) -> messages . size }
? . key // Get their names
println ( frequentSender ) // [Ma]
// Who are the senders?
val senders = messages
. asSequence () // Make operations lazy (for a long call chain)
. filter { it . body . isNotBlank () && ! it . isRead } // Use lambdas...
. map ( Message :: sender ) // ...or member references
. distinct ()
. sorted ()
. toList () // Convert sequence back to a list to get a result
println ( senders ) // [Adam, Ma]
data class Message ( // Create a data class
val sender : String ,
val body : String ,
val isRead : Boolean = false , // Provide a default value for the argument
)
val messages = listOf ( // Create a list
Message ( "Ma" , "Hey! Where are you?" ),
Message ( "Adam" , "Everything going according to plan today?" ),
Message ( "Ma" , "Please reply. I've lost you!" ),
// Tests
// The following example works for JVM only
import org . junit . Test
import kotlin . test . *
class SampleTest {
@Test
fun `test sum` () { // Write test names with whitespaces in backticks
val a = 1
val b = 41
assertEquals ( 42 , sum ( a , b ), "Wrong result for sum($a, $b)" )
fun `test computation` () {
assertTrue ( "Computation failed" ) {
setup () // Use lambda returning the test subject
compute ()
// Sources
fun sum ( a : Int , b : Int ) = a + b
fun setup () {}
fun compute () = true
Expressive
Multiplatform
data class Employee (
val name : String ,
val email : String ,
val company : String
) // + automatically generated equals(), hashCode(), toString(), and copy()
object MyCompany { // A singleton
const val name : String = "MyCompany"
fun main () { // Function at the top level
val employee = Employee ( "Alice" , // No `new` keyword
"[email protected]" , MyCompany . name )
println ( employee )