Last active
July 31, 2021 16:18
-
-
Save motaher13/ce8414ac8903b454aee405583d89282d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Kotlin | |
source: https://www.youtube.com/watch?v=F9UC9DY-vIU | |
Variables | |
* 2 types | |
1. immutable -> val | |
2. mutable -> var | |
*Global varriables are called ‘top level varaibles | |
*Data types are not nullable by default, to make it nullable, add ? at the end, String? | |
*Could auto detect type, no need to assign type, just-> var a=“5” | |
*if use auto detect, it won’t take null anymore | |
Control | |
*if else works as normal | |
*when() use as ‘switch’ | |
*abul= if(a==b) c else d; the syntax for Ternary operator | |
*abul = abul?:”abul not found” -> used to perform null check | |
*when() could also used as ternary return | |
*the operator === checks for same object, like js. == calls equals method , if have any. | |
*use ‘is’ to very type. if (Tercher is Employee){} | |
Basic Functions | |
*functions could be written outside of class, these are called ‘top level function’ | |
*fun abul():String{} how to write return type | |
*use $ to interpolate string -> “hi, i am $name” | |
*use ‘=‘ for single statement function. fun abul()= println(“asdf”) | |
Collections | |
*collections are immutable bydefault in kotlin | |
*arrayOf(2,4,5) to get array | |
*foreach, forEachIndexed, | |
*[], get() both works for array | |
*listOf() to get list | |
*lots of functions are available inside list, like filter(), filterNotNull(), associate(), forEach() etc | |
*return orEmpty() while filtereing a list, to avoid null | |
*mutableListOf() to get mutable list | |
*mapOf() just like array | |
*mutableMapOf() to get mutable map | |
*List<String> to receive a list in function | |
Vararg, named, and default parameter | |
*vararg works as … | |
*star operator works as spread operator in javascripts, could be used to pass array to vararg | |
*could mention parameter name while calling the function, so it helps to override signature | |
*equal operator is used to assign default value | |
Constructor | |
*class Person(val firstName:String=“peter”, val lastName:String=“perker”) | |
*without specifing getter and setter, variables stays accessible through name | |
*use get(),set() right after a var, with a tab | |
*everything is by defauld public | |
*access modifiers -> public=public, internal=default, private = private, protected= within the class or subclass | |
Interface | |
*There are interface, absctract class like normal | |
*interface can have default implementation of method | |
*(abul as babul).getSomething(). -> as is used to cast babul to abul | |
*compiler can auto cast, with smart casting | |
Inheritance | |
*by default class are final, to make it extendable, use ‘open’ in front of it | |
*to override a instance variable, it also have to be open | |
Object expression | |
*basically anonymous inner class | |
Companion objects | |
*a function or any member of the class that can be called without having the instance of the class then you can write the same as a member of a companion object inside the class.The companion objects can access private members of the class. Hence, they can be used to implement the factory method patterns | |
*you can access all methods of a companinon object with the outer class name | |
*they can extend and implement class/interface | |
Object Declarations | |
*a safe way to create thread safe singleton in kotlin | |
object SingletonExample { | |
... .. ... | |
// body of class | |
... .. ... | |
} | |
ENUM | |
*enum class EntityType{ | |
EASY, MEDIUM, HARD | |
} | |
*use “enumClassname.name” to get the name as string. liken EntityType.EASY.name will return “EASY” | |
*use “enumClassname.getFormattedName()” to get name in formatted string. | |
Sealed Class | |
*Sealed classes are used for representing restricted class hierarchies, when a value can have one | |
of the types from a limited set,but cannot have any other type. | |
*outer class contains ‘sealed’ keyword | |
*inner classes must extend the outer class | |
*inner classes could be instantiated from anywhere through outer.inner | |
*the outer class acts as enum, ther inner classes acts as types for enum. this could be used in when() operator to check the | |
type of object | |
Data Classes | |
*it’s a class that contains ‘data’ keyword | |
*it contains the following methods under the hoods, without writing any code | |
* equals(): Boolean | |
* hashCode(): Int | |
* toString(): String | |
* component() functions corresponding to the properties | |
* copy() | |
*thats why it’s very convenient to hold DTO type objects in data class | |
Extension Function/Properties | |
*add method to a class, from outside the class | |
fun Person.printPersonId(){} | |
Advanced Functions | |
Heiger Order functions | |
*takes or returns functions | |
*passing function is done with lambda | |
*if last parameter is lamda while calling, it could be written outside the () | |
Functional type | |
*store functoin inside a variable | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment