1. Variables
1-1 'val' and 'var'
var
is mutable, can be changed during executionval
is immutable, cannot be changed once initialized
|
|
1-2 Data Types
- kotlin is a typed language
- no need to always state the type because of the
type inference
integer types
|
|
Floating point number types
|
|
Bool, Char and String
|
|
2. Operators
2-1 Arithmetic operators
|
|
2-2 Comparison operator
- ==, !-, <=, >=...
2-3 increment, decrement
- --, ++ (before use or after use)
3. Control flows
3-1 if-else if-else, when
|
|
when
|
|
3-2 while
while
|
|
do-while
|
|
3-3 for
|
|
|
|
4. Functions
- Difference between argument and parameter?
- Difference between method and function? -- A method is a function within a class
|
|
5. Nullable
5-1 Null-safe operator
?
|
|
5-2 Elvis operator
?:
|
|
!!
converts a nullable variable to a non-null variable (will throw an NPE if the nullable variable holds a null value)
|
|
- null save operator in a chain
|
|
6. OOP in kotlin
6-1 Class
|
|
6-2 Scope and shadowing
|
|
6-3 Member variables
|
|
6-4 Lateinit & Getter Setter
- Be careful before you have initialized lateinit variables
|
|
- Getter
|
|
- Actually kotlin is generating getter/setter for us
|
|
- Custom setter
|
|
- Private setter
- getter's accessibility should be the same as the property's accessibility. (no need to explicitly make a private getter)
|
|
6-5 Data class
|
|
6-6 Inheritance
- All classes and content are
final
by default, which meansnon heritable
- Add
open
keyword to make a class heritable - ★Implemented Decorator pattern in Kotlin:
|
|
6-7 Interface
- An interface is a contract that a class may choose to sign
- Implementing an interface make a class obliged to implements its variables and functions
- Interfaces can implements from other interfaces
- Multiple implementation is allowed
|
|
6-8 Abstract class
- You cannot create objects of an abstract class. However you can inherit subclasses from an abstract class
- The members of an abstract class are non-abstract unless you explicitly use the abstract keyword
- You don't need to annotate abstract classes or functions with
open
- You can override a non-abstract open member with an abstract one.
- Different from interface:
- Interface cannot hold
states
(fields, constructors..) but abstract classes can
- Interface cannot hold
|
|
6-9 Type casting
- List
|
|
- Smart cast
|
|
- Explicit unsafe casting using
as
|
|
7. Advanced
7-1 ArrayLists
- used to create dynamic arrays
- the size of an arrayList can be increased or decreased according to your requirement
- provides both
read and write
functionalities - follows the sequence of insertion order
non synchronized
, may contain duplicate elements- Constructors
- ArrayList
() - ArrayList(capacity: Int)
- ArrayList(elements: Collection
) (filled with the elements of a collection)
- ArrayList
- Functions
- add(element: E)
- clear()
- get(index: Int)
- remove(element: E)
- Code
|
|
add list to arrayList
|
|
Iterator
|
|
7-2 Lambda expressions
- Lambda is a function which has no name.
- Lambda and anonymous functions are
function literals
(functions that are not declared, but passed immediately as an expression) - Lambda is defined with curly braces {} which takes variables as a parameter(if any) and a body of a function
- the body of a function is written after the variable(is any) followed vy
->
operator(lambda operator) - Syntax: { variable(s) -> body_of_lambda }
|
|
7-3 Visibility modifier
- 4 types:
public
,private
,protected
,internal
- public
- Is accessible from everywhere in the project
- It's a default modifier in kotlin.
- All public declarations can be placed at the top of the file
1 2 3 4 5 6 7 8 9 10
public class Example { class Demo{ } public fun hello() fun demo() public val x = 5 val y = 10 }
- private
- Allows the element to be accessible only within the block in which properties, fields, etc. are declared
- A private package can be accessible within that specific file
1 2 3 4 5
private class Example { //Accessible within the same source file private val x = 1 //Accessible within class Example private doSomething() { //Accessible within class Example } }
- Internal
- Makes the field visible only inside the module in which it is implemented
1 2 3 4 5 6 7 8 9
class Outer { var example = Example()//error: public example exposes this internal Example //make example internal or Outer class internal will solve the problem internal class Example { internal val x = 5 //Basicly can't be accessed out of Outer class internal fun getValue() { } } }
- protected
- Allows visibility to its class or subclasses only
- When overridden in subclasses is also
protected
unless it's explicitly changed - CANNOT be declared at top level -> packaged cannot be protected
1 2 3 4 5 6 7 8 9 10 11
open class Base { protected val i = 0 open protected val x = 5 } class Derived: Base() { fun getValue: Int { return i } override val x = 6 }
- public
7-4 Nested and Inner class
- Nested class: a class which is created inside another class
static
by default- cannot access the data members of outer classes(one way accessibility)
- Inner class: a class which is created inside another class with keyword inner
- Nested class + inner mark => inner class
- Cannot be declared inside interface or non-inner nested classes
- Advantage over nested class: it's able to access members of its outer class even it is private
- An inner class keeps an reference to its outer class
1 2 3 4 5 6 7 8 9 10 11
fun main() { Outer().Inner().printOut() } class Outer { private val out = "out" inner class Inner { fun printOut() { print(out) } } }
7-5 Safe cast and Unsafe cast operator
as?
returns null if casting is not possible rather than throwing a ClassCastException (see 6-9 Type casting)
7-6 Exception handling
try-catch-finally
- Unchecked Exception
- Thrown due to mistakes in our code
- Extends the RuntimeException class
- ArithmeticException, SecurityException, ArrayOutOfBoundException, NPE...
- Checked Exception
- Checked at compile time
- Extends Throwable class
- IOException, SQLException...