Programming Concepts Flashcards
When, if/else as an expression
The body must return a value but you don’t have to use return statement:
val name = if (condition) {
body 1
} else { condition
body 2
}
val name = when (parameter) {
condition 1 → body 1
condition 2 → body 2
condition 3 → body 3
}
Lambda expression
Shorthand syntax to declare function without fun keyword. You can store a lambda expression directly in a variable
val variableName ={
lambda expression
}
variableName () - to call it
?.
Save call operator
nullableVariable ?. method / property
Stops any attempt off member access to null references and return null
!!.
Not null assertion operator
nullableVariable!!. method / property
You assert that the value is not null
, - in when statement
To separate multiple conditions
condition 1, condition 2
in in when statement
in 1…10
is in when statement
To check the data type
is type → body 1
?:
Elvis operator to add default value when there is indeed null
val name = nullableVariable?.method ?: defaultValue
Class in Kotlin
ClassName {
Properties
Methods
Constructors
}
ClassName () - to create an object
ClassObj.methodName() - to call class method
Getter and setter functions
F.e.
var speakerVolume = 2
get () = field
set ( value ){
field = value
}
Variable read-only (val) don’t have set functions
Constructor
Primary - class can have only one, defined as a part of a class header
ClassName constructor() { body}
Secondary - class can have multiple with or without parameters. If the class have primary each secondary must implement it. Enclose in the class body
Open class
Informs the compiler that this class is extendable
Inheritance
SubclassName ( optional parameters) : superclass (optional parameters) {
Body
}
To override functions:
- in superclass: open fun
- in subclass override fun
this same can be used for properties
Super - to reuse superclass code in subclass
var objName : superClassName = subclassName () - object of subclass
Visibility modifiers
- public (default) accessible everywhere
- private accessible in the same class or source file
- protected accessible in subclass
- internal accessible in this same module
Modifier var name: date type = initial value
Modifier class name { body }
Module
A collection of source files and build settings that let you divide your project into discrete units of funcionality
Package
A directory or folder that groups related classes, a module can contain multiple packages
Functions as data type
They can be stored in variables, passed into functions and returned from functions.
To store a function in variable and refer to it as value use :: ( function reference operator )
val variable = :: functionName
val name:() → Unit ={
body
}
Lambda expression with parameters
Names in order that they occurs
val functionName = { param1, param2 →
function body
}
When it has only one parameter be can omit its name and use it
Function with return type
funcName (optional parameters) → returnType {
body
}
If return type is nothing () → Unit
Function as a return type
fun functionName (): functionType {
return nameOfAnotherFunc
}
Passing a function as a parameter to other function
If this function has marcumeters too no names only types after comma
fun funcName ( paramName : type, funcName : (paramType, …) → returnType) : () → Unit { }
Trailing lambda syntax
When a function type is a last parameter
f.e. trickOrTreate(false) lambda expression
repeat()
Function to express for loop
repeat( times) {
…
}
random ()
To get random number
var result = (1..6).random()