Skip to content

Commit bb620ce

Browse files
Update README.md
1 parent 913d770 commit bb620ce

File tree

1 file changed

+375
-0
lines changed

1 file changed

+375
-0
lines changed

_moreReadMe/kotlin_basics/README.md

Lines changed: 375 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,3 +854,378 @@ fun main(args : Array<String>) {
854854
As you can see, the constructor parameter n and a are being used to initialized their respective properties.
855855

856856
**Data classes** are kotlins way of providing concise immutable data types... it is going to generate equals hashcoded to string... they will be considered equal if data they contain is equal... also, they give us effective copy construcors... x.copy()
857+
858+
859+
860+
861+
862+
863+
864+
865+
866+
867+
868+
869+
870+
871+
872+
873+
874+
875+
876+
---
877+
878+
879+
880+
881+
882+
883+
884+
```kotlin
885+
sayHello1()
886+
sayHello2()
887+
888+
fun sayHello1(): Unit { println("Hello")}
889+
890+
fun sayHello2() { println("Hello")}
891+
892+
fun getGreeting(): String = "Hello Kotlin" //single expression function
893+
fun getGreeting() = "Hello Kotlin" //also, single expression function
894+
895+
fun sayHello3(itemToGreet: String) {
896+
val msg = "Hello " + itemToGreet // or val msg = "Hello $itemToGreet"
897+
println(msg )
898+
}
899+
900+
fun sayHello3(itemToGreet: String) { println("Hello $itemToGreet")}
901+
902+
fun sayHello3(itemToGreet: String) = println("Hello $itemToGreet")
903+
904+
fun sayHello3(greeting:String, itemToGreet: String) = println("$greeting $itemToGreet")
905+
906+
907+
908+
909+
910+
fun sayHello4(greeting:String, itemsToGreet: List<String>) {
911+
itemsToGreet.forEach { itemToGreet ->
912+
println("$greeting $itemToGreet")
913+
}
914+
}
915+
916+
sayHello4("Hi", things2)
917+
918+
// varargs, named arguments, default param values
919+
920+
sayHello4("Hi", listOf())
921+
922+
923+
924+
fun greetPerson(greeting:String, name: String) = println("$greeting $name")
925+
greetPerson(greeting:"hi", name:"John")
926+
greetPerson(greeting = "hi", name = "John") // named arguments syntax
927+
greetPerson(name = "John", greeting = "hi") // any order possible, but must take both arguments; can also be used forr varargs
928+
929+
fun greetPerson2(greeting:String = "Hello", name: String = "Kotlin") = println("$greeting $name") // default param values
930+
greetPerson2(name = "John")
931+
932+
//This helps us replicate builder pattern without writing getters, seetings, private coonstructors, etc
933+
934+
class Person // constructor not required
935+
class Person constructor() // empty primary construtor
936+
class Person() // empty primary construtor
937+
---
938+
val person = Person() // new keyword not required
939+
940+
class Person2(firstName:String, lastName:String)
941+
---
942+
val person = Person("John", "Thomson")
943+
944+
class Person2(_firstName:String, _lastName:String) { // primary contrutor
945+
val firstName: String // properties
946+
val lastName: String
947+
948+
init {
949+
firstName = _firstName
950+
lastName = _lastName
951+
}
952+
}
953+
or
954+
class Person2(_firstName:String, _lastName:String) { // primary contrutor
955+
val firstName: String = _firstName
956+
val lastName: String = _lastName
957+
}
958+
or
959+
class Person2(val firstName:String, val lastName:String) // primary contrutor
960+
961+
val person = Person2("John", "Thomson")
962+
println(person.lastName)
963+
964+
class Person2(val firstName:String, val lastName:String) {
965+
966+
init { println("init 1") }
967+
968+
construtor(): this("Peter", "Parker") { // secondary constructor with calling primary constructor
969+
println("seconndary constructor")
970+
}
971+
972+
init { println("init 2") }
973+
}
974+
or
975+
class Person2(val firstName:String = "Peter", val lastName:String = "Parker")
976+
977+
val person = Person2() // output init1 init2 seconndary constructor
978+
979+
class Person2(val firstName:String = "Peter", val lastName:String = "Parker") {
980+
var nickName: String? = null
981+
}
982+
983+
person.nickName = "Shades"
984+
985+
class Person2(val firstName:String = "Peter", val lastName:String = "Parker") {
986+
var nickName: String? = null
987+
set(value) { // overriding setter
988+
field = value;
989+
}
990+
get() { // overriding getter
991+
println("returned value is $field")
992+
return field;
993+
}
994+
995+
fun printInfo() {
996+
val nick = if(nikName != null) nickName else "no nickname"
997+
val nick = nickName ?: "no nickname" // same as above using elvis operator
998+
}
999+
}
1000+
1001+
person.printInfo()
1002+
1003+
1004+
interface PersonInfoProvider
1005+
class BasicInfoProvider: PersonInfoProvider
1006+
1007+
interface PersonInfoProvider {
1008+
fun printInfo(person: Person)
1009+
}
1010+
1011+
abstract class BasicInfoProvider: PersonInfoProvider
1012+
1013+
class BasicInfoProvider2: PersonInfoProvider {
1014+
override fun printInfo(person: Person) {
1015+
// super.printInfo() // not compulsory in this case
1016+
}
1017+
}
1018+
1019+
val provider = BasicInfoProvider2()
1020+
1021+
class D: interface A, interface B, interface C
1022+
1023+
if(infoProvider is SessionInfoProvider) ...
1024+
else ...
1025+
1026+
if(infoProvider !is SessionInfoProvider) ...
1027+
else ...
1028+
1029+
if(infoProvider is SessionInfoProvider) {
1030+
(infoProvider as SessionInfoProvider).getSessionId // manual type casting
1031+
infoProvider.getSessionId // smart casting
1032+
}
1033+
else ...
1034+
1035+
class Fancy: BasicInfoProvider() // inheritance
1036+
1037+
val provider = object : PersonInfoProvider { // object express anonymous inner class; for eg. can be used for click listener
1038+
...
1039+
new fun
1040+
}
1041+
1042+
suppose we want to create factory to create entity - we can use companion object
1043+
a companion object is scoped to an instance of another class
1044+
1045+
class Entity private construtor(val id: String) {
1046+
companion object { // can be rename like "comapanion object Factory"
1047+
fun create() = Entity("id")
1048+
}
1049+
}
1050+
1051+
val entity = Entity.Companion.create() // same as Entity.create(), writing COmpanion is not required
1052+
val entity = Entity.Factory.create() // suppose we have named it Factory
1053+
1054+
companion objects are like any other class, an implement interface or inherit or have properties or methods
1055+
1056+
object declaration is a convenient way of creating threaad safe singletons within kt
1057+
1058+
object EntityFacctory {
1059+
fun create() = Entity("id")
1060+
}
1061+
class Entity (val id: String) {
1062+
}
1063+
1064+
enum class EntityType {
1065+
EASY, MEDIUM, HARD
1066+
}
1067+
1068+
val id = UUID.randomID().toString()
1069+
1070+
sealed classes allow us too define related class hierarhies... for eg. can be used where result is "success or failure" or "easy, medium or hard"
1071+
with sealed classes we can have different propeerties (key diff bw enum & sealed)... compiler will use smart casting
1072+
1073+
1074+
sealed class Entity() {
1075+
data class Easy(val id: String, val name: String): Entity() // data classes
1076+
data class Medium(val id: String, val name: String): Entity()
1077+
data class Hard(val id: String, val name: String, val multiplier: Float): Entity()
1078+
}
1079+
1080+
fun Entity.Medium.printInfo() { // extension function
1081+
...
1082+
}
1083+
1084+
fun printFilteredStrings(list: List<String>, predicate: (String) -> Boolean) { // higher order funstion
1085+
list.forEach {
1086+
if(predicate(it)) {
1087+
println(it)
1088+
}
1089+
}
1090+
}
1091+
1092+
val list = listOf("Kotlin", "Java", "C++", "Javascript")
1093+
printFilteredStrings(list, {it.startsWith("K")}) // we have passed our lambda withing parenthesis
1094+
1095+
printFilteredStrings(list) { // we can take use of lambda syntax, same as above
1096+
it.startsWith("K")
1097+
}
1098+
1099+
val list = listOf("Kotlin", "Java", "C++", "Javascript", null, null)
1100+
list
1101+
.filterNotNull()
1102+
.take(3) // or takeLast(3) to take that many elements
1103+
.filter {
1104+
it.startsWith("J")
1105+
}
1106+
.map {
1107+
it.length
1108+
}
1109+
.forEach {
1110+
println(it)
1111+
}
1112+
1113+
output: 4 or 10 (for last 3)
1114+
1115+
list
1116+
.filterNotNull()
1117+
.associate {it t it.length}
1118+
.forEach {
1119+
println("${it.value}, ${it.key}")
1120+
}
1121+
1122+
val map = list
1123+
.filterNotNull()
1124+
.associate {it t it.length}
1125+
1126+
the kt standard lib also provides a variety of fun to help us pull out individual elements from a collection
1127+
1128+
val language = list.first() // or .last()
1129+
1130+
val language = list.filterNotNull().find{it.startsWith("Java")} // or .findLast {}
1131+
1132+
strings in kt have a useful fun called orEmpty
1133+
val language = list.filterNotNull().find{it.startsWith("foo")}.orEmpty() // to default collection to empty instead of null
1134+
1135+
kt has 1st class supp for fun, inluding fun types and higher order fun... and kt standard lib builds upon those tools and provides a rich set of fun operator for us to use... this allows us to build powerful functional chains to transform our data and make complex workflows much simpler
1136+
1137+
1138+
1139+
1140+
1141+
1142+
1143+
model data
1144+
Kotlin proj default
1145+
--
1146+
1147+
top level variables and functions & var and fun associated with class
1148+
--
1149+
1150+
toString(), hashCode(), ...
1151+
1152+
we have extension function or extension properties on existing class
1153+
1154+
higher order funtions & functional data type
1155+
1156+
invoke() method....
1157+
1158+
functional types...
1159+
1160+
these types of functions will be useful in click & event listeners etc
1161+
1162+
kotlin standard library
1163+
1164+
1165+
1166+
1167+
⭐️ Course Contents ⭐️
1168+
⌨️ (0:00:50​) Create Your First Kotlin Project
1169+
⌨️ (0:04:23​) Hello World
1170+
⌨️ (0:06:33​) Working With Variables
1171+
⌨️ (0:11:04​) Type System
1172+
⌨️ (0:15:00​) Basic Control Flow
1173+
⌨️ (0:21:31​) Basic Kotlin Functions
1174+
⌨️ (0:27:12​) Function Parameters
1175+
⌨️ (0:32:52​) Arrays
1176+
⌨️ (0:35:28​) Iterating with forEach
1177+
⌨️ (0:41:17​) Lists
1178+
⌨️ (0:42:47​) Maps
1179+
⌨️ (0:45:05​) Mutable vs Immutable Collections
1180+
⌨️ (0:49:24​) Vararg Parameters
1181+
⌨️ (0:54:21​) Named Arguments
1182+
⌨️ (0:56:26​) Default Parameter Values
1183+
⌨️ (1:00:27​) Create A Simple Class
1184+
⌨️ (1:03:35​) Adding Class Properties
1185+
⌨️ (1:05:15​) Class Init Block
1186+
⌨️ (1:06:40​) Accessing Class Properties
1187+
⌨️ (1:07:32​) Primary Constructor Properties
1188+
⌨️ (1:08:17​) Secondary Constructors
1189+
⌨️ (1:09:50​) Working With Multiple Init Blocks
1190+
⌨️ (1:11:30​) Default Property Values
1191+
⌨️ (1:11:59​) Properties With Custom Getters/Setters
1192+
⌨️ (1:16:52​) Class Methods
1193+
⌨️ (1:20:12​) Visibility Modifiers - Public/Private/Protected/Public
1194+
⌨️ (1:22:30​) Interfaces
1195+
⌨️ (1:24:21​) Abstract Classes
1196+
⌨️ (1:26:13​) Implementing An Interface
1197+
⌨️ (1:26:35​) Overriding Methods
1198+
⌨️ (1:28:30​) Default Interface Methods
1199+
⌨️ (1:29:30​) Interface Properties
1200+
⌨️ (1:31:40​) Implementing Multiple Interfaces
1201+
⌨️ (1:32:57​) Type Checking And Smart Casts
1202+
⌨️ (1:36:18​) Inheritance
1203+
⌨️ (1:43:07​) Object Expressions
1204+
⌨️ (1:45:06​) Companion Objects
1205+
⌨️ (1:49:51​) Object Declarations
1206+
⌨️ (1:52:41​) Enum Classes
1207+
⌨️ (1:58:16​) Sealed Classes
1208+
⌨️ (2:00:07​) Data Classes
1209+
⌨️ (2:12:25​) Extension Functions/Properties
1210+
⌨️ (2:16:40​) Higher-Order Functions
1211+
⌨️ (2:29:07​) Using The Kotlin Standard Library
1212+
1213+
1214+
1215+
1216+
1217+
1218+
private lateinit var // because late initialization, private so that it can be referenced
1219+
viewholder provides access to all views of one elemenet of recycler view
1220+
kotlin.math//
1221+
companion object are singleton varible defined constant //similar to static in java
1222+
we an access its memebers directly through the ontaining class
1223+
.shuffled()
1224+
data class
1225+
!!
1226+
interface//
1227+
delegate
1228+
color shades
1229+
setup recyclerview//
1230+
let
1231+
```

0 commit comments

Comments
 (0)