- Kotlin is an OOP, fonctionnel, static typed programming language.
- Kotlin is developed by JETBRAINS (enginers team).
- Kotlin is used in backend, frontend and mobile dev.
- Prefered Kotlin's features by developers:
Syntax:
More simple syntax than java.
No need for semicolons.
Params can have by default values.
Vars can be declared whith no type.
Null-safety:
No NullPointerException at compilation time.
Vars are not by default null.
Type Inference:
Vars types can be concluded at compilation time from given values
whitout explisitly specifiying type.
Lamdas and fonctionnel porgramming:
Presense of lamda language for funs defs + high level function.
Compatibilité with Java:
For java devs, they can easily switch to kotlin + you can use java
predefined libaries.
- Kotlin = less code lines than Java.
- Kotlin files extension: .kt (compiled code), .kts (interpred code).
- .kt ----(Kotlin Complier)----> IR ----(LLVM)----> binary
- LLVM (Low Level Virual Machine) is used to transforme IR kotlin code to a native
binary.
- IR (Intermediate Representation) is the getted form just after compliation time
before passing to the LLVM.
- Output:
println("Hello World")
- Vars:
syntax -> val/var var_name[: type] = value
val is for immutable referencing.
var is for mutable referencing.
variables in Kotlin must be initialized in delcaration.
get var type -> var_name::class.simpleName
Double vars values must have a '.' to indicate double values.
Float vars values must have a 'F' at the end to indicate floating values.
We can get num value for a Char var using .toInt(), but Chars can not be
declared as nums.
Boolean vars accept true and false as values.
Concatenation can be done using .plus()
String are treated as arrays of chars, accessing chars of strings can be done
using indexing.
Strings methods + attrs:
.length
.lowercase()
.uppercase()
.indexOf(String)
.get(index)
.compareTo(string) -> 0 for equality
.substring(start, end) ; end is excluded
.split(delemitor)
.startsWith(string)
String concatenation:
method 1 : using +
method 2 : using .plus(string) method
method 3 : "$str1 added to $str2" <- string template
method 4 (for StringBuilder) : .append(string)
- Collections:
listOf(items) is used to create and return a immuatable list.
-> methods + attrs:
.size
.first()
.last()
.min()
.max()
.filter(condition) ; in condition, we refere to current item
using 'it'
.sorted()
mutableListOf<T>(items) is used to create and return a muatable list.
-> if you're calling the mutableListOf() with no params, type must be
specified (no inference type possible).
methods:
.add()
.remove()
arrayOf<T>(items) is used to create static (but not immuatable) array (you
can't change size, but you can change items values).
HashMap<K, V>(key1 to value1, ...)
methods:
.put(key, value)
- Matrix:
declaration:
val matrix = arrayOf(
intArrayOf(1, 2, 3),
intArrayOf(4, 5, 6),
intArrayOf(7, 8, 9)
)
access: indexing
.contentToString() can be appield on an array to get a [item1, item2, ...]
string representation
- Input:
readLine() // return a String?
readln() // return a String (no null check needed)
readln().toInt()
readln().toDouble()
- NULL SAFETY:
? -> is added to the type in vars declaration to make the var Nullable (can
accept null values).
var str: String? = null
?. -> safe call (access), it's used to avoid NullPointerException when trying
to access attrs or method of null objects, it's executed only if the object is not
null.
person?.name -> return null when person is null instead of a
NullPointerException.
?: -> Elvis operator is used to assign default value for null values.
val result = someNullableValue ?: defaultValue
!! -> not-null assertion is used to force a NullPointerException at runtime
when the value is null.
(punish used for null)
var name_copy = name!!
- Functions:
declaration:
fun fun_name(param_name: param_type, ...): return_type {
// fun_body
}
calling:
fun_name(param1, param2, ...)