Kotlin 101 practice
Kotlin 101 practice
How to create a kotlin project IntelliJ IDEA
- click File -> New -> Kotlin, IDEA will help to create kotlin project with JVM
- You also can use REPL (Read-Eval-Print-Loop) interactive shell to practice the basic kotlin syntax
Kotlin basic syntax
- Kotlin is very similar to java language, but also there is several difficulties between them.
- using
fun
to define a function - no semicolons in the end of a line
- using parentheses for function arguments
- using curly brace to define function block
- using
- Syntax
- operator
var
andval
, var is for mutable definition, val is for immutable definition. - define variable type:
var rocks: Int = 1
, type is behind the variable and divided by colon. - nullability
- using operator
?
to define a nullable variable - using operator
!!
to make a not-null announcement
- using operator
- list
- listOf is immutable
- mutableListOf is mutable
- array: create array by build-in function
arrayOf
- loop
- do/ while
- foreach
- repeat
1
2
3repeat(2) {
println("A fish is swimming")
}
- function
- without return value
1
2
3fun feedTheFish() {
println("test")
} - with return value
1
2
3
4fun feedTheFishWithValue(): String {
val test = "test string"
return test
} - default value of parameter
1
2
3fun functionWithDefaultValueArgument(speed: String = "test speed") {
println("test argument value $speed")
} - lambdas
var lambdas = { inputValue: Int -> inputValue / 2}
- higher-order-functions: passing a lambda to another function
- return value
1
2
3fun returnValue(x: Int, y: Int, operate: (Int, Int) -> Int): Int {
return operate(x, y)
}- you pass a lambdas, function or just a simple value as an input to a function
- lambdas
1
2var sum = {(x: Int, y: Int) -> x + y}
returnValue(1, 2, sum) - regular function
1
2
3
4
5fun sum(x: Int, y:Int): Int {
return x + y
}
returnValue(1, 2, ::sum) - simple value
- return function
1
2
3fun operate(): (Int, Int) -> Int {
return sum //::sum '**sum**' is defined as a regular function, the former is a lambda expression
} - extensions, only can access to public api
1
2
3
4Class.extensionFunctionName(arg: type): returnValue {
return this.value
}
- return value
- OO related knowledge
- class
- no needs to create getter and setter, they are created automatically.
- no needs to create an individual constructor method, rather than defining a constructor with class directly
1
2
3
4class MyClass(arg1: String, arg2: String) {
var arg1Internal1: String = arg1
var arg1Internal2: String = arg2
} - providing init block in class constructor, you can write some logic in the init block
- providing secondary constructor in class
1
2
3
4
5
6
7
8class MyClass(arg1: String, arg2: String) {
var arg1Internal1: String = arg1
var arg1Internal2: String = arg2
constructor(arg: Int): this() {
//your logic
}
}
- subclass
- by default kotlin cannot be subclassed, otherwise adding operator
open
before your class, the same as the property and member
- by default kotlin cannot be subclassed, otherwise adding operator
- inheritance
- if the setter is defined as following block, it will throw
Exception in thread "main" java.lang.StackOverflowError
1
2
3
4
5override var shape: String
get() = towerTankShape
set(value) {
shape = value
}
- if the setter is defined as following block, it will throw
- delegation
- declare an object as a singleton object which can set default color value and doesn’t need
to override super property1
2
3
4
5
6
7object GolderColor: FishColor {
override val color = "golder"
}
class ImplementClass(fishColor: FishColor = GolderColor): Fishcolor by fishColor {
}
- declare an object as a singleton object which can set default color value and doesn’t need
- special class
- special data structure
- pairs connect with two values by to keyword
- pair can be constructed and deconstructed
1
2
3
4
5val pair = "part one" to "part two"
println("${pair.first} with ${pair.second}")
// nested pair
("out part one" to "out part two") to "inside part two"
- pair can be constructed and deconstructed
- triples is similar with java
- const:
- const keyword only can be used in object singleton class
- using const in class
1
2
3
4
5class MyClass {
companion object {
const val CONSTANT3 = "constant in companion"
}
}
- pairs connect with two values by to keyword
- generic
- class: how to define a generic class
1
class GenericClass<T> (val specificClass: T)
- usage
1
2
3GenericClass<SpecificClass>(SpecificClass())
// the class name in angle bracket can be removed as follow
GenericClass(SpecificClass()) - methods
- functions
- class: how to define a generic class
- annotation
- add annotation key work before the class
1
annotation class KotlinClass
findAnnotation
method to get target annotation- target annotation, add @Target before the annotation class
1
2
annotation class OnGet - then if this function scans related annotation, the later code block will add some logic
- target annotation, add @Target before the annotation class
::class
is used to reflect the class internal information.- tips: you need to import
implementation("org.jetbrains.kotlin:kotlin-reflect:1.6.21")
- tips: you need to import
@
keyword can be used as a labeled break, which can control the program flow
1
2
3
4
5
6
7
8
9fun test() {
outstop@ for (i in 1 .. 100){
print("$i")
for (j in 1 .. 100){
if (i > 10)
break
}
}
} - add annotation key work before the class
- class
- without return value
- operator
- Title: Kotlin 101 practice
- Author: Xiao Qiang
- Created at : 2023-03-05 14:45:27
- Updated at : 2025-03-08 10:49:30
- Link: http://fdslk.github.io/tech/kotlin/101/2023/03/05/kotlin-101/
- License: This work is licensed under CC BY-NC-SA 4.0.
Comments