Jul 25, 2024
Install JetBrains IDE (IntelliJ IDEA)
Create Your First Kotlin Project
hello_kotlin
main.kt
, for your codeHello World Example
main
function:
fun main() {
println("Hello Kotlin")
}
main
function structure.var
(reassignable)val
(assigned once)val name: String = "Nate"
var age: Int = 25
?
for nullable types.if (condition) {
// do something
} else {
// do something else
}
when (variable) {
condition -> action
else -> default
}
fun
keyword:
fun greet(name: String): String {
return "Hello, $name"
}
fun greet(name: String = "Guest")
.class Person(val name: String, var age: Int) {
fun printInfo() {
println("Name: "+name+", Age: "+age)
}
}
val person = Person("Nate", 25)
person.printInfo()
open
to allow classes to be inherited.equals
, hashCode
, and toString
.map
, filter
val numbers = listOf(1, 2, 3, 4, 5)
val even = numbers.filter { it % 2 == 0 }
fun <T> List<T>.customFilter(predicate: (T) -> Boolean): List<T> {
val result = mutableListOf<T>()
for (item in this) {
if (predicate(item)) {
result.add(item)
}
}
return result
}