Kotlin was developed internally by Jetbrains, and it was revealed in July 2011 as a new programming language for the JVM. In February 2012, Jetbrains open sourced the project under the Apache 2 license. The main goal of the project was to write Jetbrains products like IntelliJ IDEA. The first stable version was released in February 2016. During Google I/O 2017, Google announced the support for Kotlin on Android. Since then, Kotlin has been the official recommended language for Android development. What is Kotlin?
Kotlin is an open source programming language, maintained by Jetbrains, Google and the community, and it's a modern language with powerful features like null-safety and asynchronous programming. What is Kotlin?
Kotlin has a great ecosystem, such as first-class support for Android development, backend development powered by Ktor and multiplaform development (JVM, Javascript, Native). Kotlin also has a great community, counting +30k stars on main Github repository, +30k members on official Slack and +50k questions on Stackoverflow. In 2018, Stackoverflow published a survey that introduced Kotlin at the second position about the most loved programming languages. Kotlin ecosystem and community
A lot of big companies are adopting Kotlin in their projects, such as Evernote, Coursera and Atlassian. And here at Brazil, we have a lot of companies using Kotlin, such as Aegro, Creditas, C6 Bank, Hash Payments, Itaú and so on. And a lot of popular open source projects are adding support for Kotlin, such as Gradle and Spring. Kotlin adoption
Basic syntax Declaring a main function with no args: fun main() { val greeting: String = "Developer Student Club" println("Hello, " + greeting) } * In Kotlin, the args parameter in main function isn’t mandatory.
Basic syntax Join strings using String Templates: fun main() { val greeting: String = "Developer Student Club" println("Hello, $greeting”) } In terms of syntax, there is a notable difference, since using templates, you compose the string in a more fashion way. In terms of runtime, there is no big difference, since the Kotlin compiler produces the same code for both string concatenation using + operator and string templates.
Basic syntax Control flow using if/else statement: fun someBusinessLogic() { if (myCondition) { !// execute some action } else if (anotherCondition) { !// execute another action } else { !// alternative flow goes here } }
Basic syntax Assignment using if/else statement: fun isLoading(value: Boolean) { val visibility = if (value) View.VISIBLE else View.GONE progressBar.visibility = visibility } * Kotlin does not have ternary operator.
Basic syntax Declaring an indexed for loop: fun printAllGuests(guests: List) { for ((index, guest) in guests.withIndex()) { println("Hello, #$index - $guest") } }
Properties and functions Declaring a mutable property: var name: String = "Wellington" name = "Chico" !// it works well since var properties can be reassigned
Properties and functions Declaring an immutable property: val age: Int = 25 age = 20 !// it produces a compiler error because val properties cannot be reassigned Immutable states are a good way to keep a consistent state during program execution. If you want a new value, you need to produce a new state instead of changing the current one.
Properties and functions Declaring a function with a return and parameter with default value: fun greetingFor(guest: String = "Chico"): String { return "Hello, $guest!" }
Properties and functions Making an extension function to use infix notation: infix fun Double.pow(exponent: Double): Double { return java.lang.Math.pow(this, exponent) } fun main() { 2.0 pow 10.0 }
Classes, interfaces and objects Declaring a data class: data class Person(var username: String, var age: Int) Data classes are a good way to represent some kind of data in your program with less boilerplate. By default, data classes implements equals(), hashCode() and toString() functions.
Classes, interfaces and objects Implementing an interface: interface PersonRepository { fun findAll(): List fun findBy(name: String): List } class PersonRepositoryImpl : PersonRepository { override fun findAll(): List { TODO("Not yet implemented") } override fun findBy(name: String): List { TODO("Not yet implemented") } }
Classes, interfaces and objects Declaring a Kotlin object: object DateUtils { private val brazilianDateFormat = SimpleDateFormat("dd/MM/yyyy") private val unitedStatesDateFormat = SimpleDateFormat("mm-dd-yyyy") fun createDateInBrazilianFormat(value: String): Date { return brazilianDateFormat.parse(value) } fun createDateInUnitedStatesFormat(value: String): Date { return unitedStatesDateFormat.parse(value) } } fun main() { DateUtils.createDateInBrazilianFormat("16/01/2021") DateUtils.createDateInBrazilianFormat("01-16-2021") }