Guest User

Untitled

a guest
Mar 26th, 2018
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.90 KB | None | 0 0
  1. ## Kotlin design decision :
  2. 1. Replace java with modern programming construct (like high order function, lambda, etc)
  3. 2. Concise, Expresive, Toolable, Interopbale, and Pragmatic (most important)
  4. 3. Statically typed language, targeted on JVM (primary) (expanded to javascript and native (under investigation)
  5. 4. Similar with other modern language (quick ramp-up time).
  6. 5. Interopable with Java (100% interopability)
  7.  
  8. ## Introduction to JVM :
  9. JVM :
  10. - Abstract machine running Java application
  11. - Single specification (multiple implementation : Open JDK most common -> based on Sun Java)
  12. - An instance is what runs a Java application
  13. - Application compiled into Bytecode
  14. - JRE (for running application) and JDK (for development)
  15. - Polyglot language platform (compile to bytecode or transpile to Java first)
  16.  
  17. ## Kotlin REPL (kotlinc)
  18. ```kotlin
  19. fun hello(name: String) {
  20. println("Hello $name")
  21. }
  22. ```
  23.  
  24. ## Kotlin Application
  25. to compile (produce MainKt.class)
  26. ```
  27. >> kotlinc Main.kt
  28. ```
  29.  
  30.  
  31. to run (need kotlin-runtime.jar)
  32. ```
  33. >> java -cp {path to kotlin runtime} MainKt
  34. ```
  35.  
  36.  
  37. to compile into jar with kotlin-runtime
  38. ```
  39. >> kotlinc Main.kt -include-runtime -d hello.jar
  40. >> kotlin hello.jar
  41. ```
  42.  
  43. ## Kotlin Basic
  44.  
  45. Declaring variable
  46. ```kotlin
  47. var streetNumber: Int // mutable variable with type int
  48. var streetName: "High Street" // use type inferrence
  49. val zip = "10210" // immutable variable
  50. ```
  51. Type Variable
  52. ```kotlin
  53. val myLong = 10L
  54. val myFloat = 20.0F
  55. val myHex = 0x0F
  56. val myBinary = 0xb01
  57. val myInt = 100
  58. ```
  59.  
  60. Conversion
  61. ```kotlin
  62. val someInt = 100
  63. val someLong: Long = someInt.toLong() // no implicit conversion
  64. ```
  65.  
  66. String
  67. ```kotlin
  68. val oneLine = "a line.\n"
  69. val multipleLine = """
  70. This is a string
  71. with multiple line
  72. """
  73.  
  74. val name = "abcde"
  75. val interpolation = "$name have ${name.length()} characters"
  76. ```
  77.  
  78. Loop and ranges
  79. ```kotlin
  80. for (a in 1..100) print(a)
  81. for (a in 100..1) print(a)
  82. for (a in 100 downTo 1) print(a)
  83. for (a in 100 downTo 1 step 5) print(a)
  84.  
  85. for (capital in listOf("London", "Paris", "Madrid")) {
  86. println(capital)
  87. }
  88.  
  89. // also support java loop like : while, do while, break label, continue
  90. ```
  91.  
  92. Conditional
  93. ```kotlin
  94. val myString = "string"
  95. if (myString != "") println("not empty")
  96. val result = if (myString != "") 10 else 0
  97.  
  98. when (result) {
  99. is "string" -> println("is string")
  100. is String -> println("not string")
  101. }
  102. ```
  103.  
  104. Packages and import
  105. ```kotlin
  106. // package and import similar in java
  107. ```
  108.  
  109. ## Functions
  110.  
  111. ```kotlin
  112. fun hello(): Unit { // Unit is void in java
  113. println("Hello")
  114. }
  115.  
  116. fun throwException(): Nothing { // Nothing represent value that never exist
  117. throw Exception("This function throws an exception")
  118. }
  119.  
  120. fun sum(x: Int, y: Int) = x + y // single expression function (with type inference)
  121. ```
  122.  
  123. Default & named parameter
  124. ```kotlin
  125. fun printProfile(name: String, email: String, phone = "555") { // use default parameter for phone
  126. println("name : $name, email : $email, phone : $phone"
  127. }
  128.  
  129. printProfile(name = "nobita", phone = "444", email = "nobita@rcti.com") // using named argument
  130. ```
  131.  
  132. Unlimited argument (vararg)
  133. ```kotlin
  134. fun printAll(vararg strings: String) {
  135. for (string in strings) println(string)
  136. }
  137.  
  138. printAll("ab")
  139. printAll("ab", "cd", "ef")
  140. ```
  141.  
  142. ## Classes in Kotlin
  143.  
  144. ```kotlin
  145. class Customer( { //in kotlin class don't have field, only property
  146. var id = 0
  147. var name: String = ""
  148. }
  149.  
  150. class Person(var name: String, var age: Int) // don't need class body
  151. // Using constructor property (must use var/val,
  152. // if not it will be treated as value passed into constructor)
  153. // use val : for read-only property
  154.  
  155. // use init block for property initialization
  156. class People(var name: String) {
  157. init {
  158. name = name.toUpperCase()
  159. }
  160.  
  161. // create secondary constructor
  162. constructor(name: String, email: String): this(name) {
  163. name = "$name, $email"
  164. // this block behave like init block
  165. // will be executed after first init block
  166. }
  167. }
  168.  
  169. fun main(args: Array<String>) {
  170. val customer = Customer()
  171. customer.id = 10
  172. customer.name = "coco"
  173.  
  174. val person = Person("suneo", "9")
  175. println("name: ${person.name}, age: ${person.age}")
  176. }
  177. ```
  178.  
  179. Custom Getter and Setter
  180. ```kotlin
  181.  
  182. class People(val name: String, val birthYear: Int) {
  183. val age: Int
  184. get() = Calendar.getInstance().get(Calendar.YEAR) - birthYear
  185.  
  186. var securityNumber: String = ""
  187. set(value) {
  188. if (!value.startWith("SN")) {
  189. throw IllegalArgumentException("security number must start with SN")
  190. }
  191. field = value
  192. }
  193. }
  194. ```
  195.  
  196. Class Function
  197. ```kotlin
  198. class People(val name: String, val birthYear: Int) {
  199. fun info(): String {
  200. return "$name, ($birthYear)"
  201. }
  202. }
  203. ```
  204.  
  205. Visibility Modifier
  206. 1. Have 4 visibility modifier : public, private, protected, internal.
  207. 2. Top level declarations
  208. - public : default visibility modifier and anywhere accessible
  209. - private : available inside file containing declaration
  210. - internal : anywhere inside the module.
  211.  
  212. 3. Classes
  213. - public : any client who sees the declaring class sees its public members
  214. - private : means visible inside this class only (including all its members)
  215. - protected : same as private + visible in subclasses too
  216. - internal : any client inside this module who sees the declaring class sees its internal members
  217.  
  218. Data classes
  219. ```kotlin
  220. data class Customer(var id: Int, var name: String, var email: String)
  221.  
  222. fun main(args: Array<String>) {
  223. val customer1 = Customer(1, "Hadi", "hadi@gmail.com")
  224. val customer2 = Customer(1, "Hadi", "hadi@gmail.com")
  225. val customer3 = Customer(2, "Hadi", "hadi@gmail.com")
  226.  
  227. val equal = customer1 == customer2 // true
  228. val notEqual = customer1 != customer3 // true
  229.  
  230. customer4 = customer1.copy() // copy all value
  231. customer4 = customer1.copy(name = "Hodi") // copy with modified name
  232. }
  233. ```
  234. you can also override hashcode() and toString() in data class
Add Comment
Please, Sign In to add comment