├── .github └── FUNDING.yml ├── .idea ├── .name ├── GettingStartedKotlin2.iml ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── libraries │ ├── Maven__junit_junit_4_11.xml │ ├── Maven__org_hamcrest_hamcrest_core_1_3.xml │ ├── Maven__org_jetbrains_kotlin_kotlin_runtime_1_0_0_beta_1038.xml │ ├── Maven__org_jetbrains_kotlin_kotlin_stdlib_1_0_0_beta_1038.xml │ ├── spek_0_1_127.xml │ └── spek_0_1_68.xml ├── misc.xml ├── modules.xml ├── uiDesigner.xml └── vcs.xml ├── .travis.yml ├── README.md ├── pom.xml └── src └── main ├── java ├── booleanOperators │ ├── classBooleanOperators.iml │ └── src │ │ ├── BooleanOperator.kt │ │ └── mainBooleanOperators.kt ├── classBasics │ ├── classBasics.iml │ └── src │ │ ├── CustomerClass.kt │ │ ├── Employee.kt │ │ └── mainClass.kt ├── conditionals │ ├── Conditionals.iml │ └── src │ │ ├── Math.kt │ │ └── mainConditionals.kt ├── controlFlow │ ├── _For │ │ └── main_For.kt │ ├── _when │ │ └── mainWhen.kt │ ├── _while │ │ └── mainWhile.kt │ ├── controlFlow.iml │ └── returnsAndJumps │ │ └── mainReturnJump.kt ├── dataClasses │ ├── dataClasses.iml │ └── src │ │ ├── Customer.kt │ │ ├── DataCustomer.kt │ │ └── mainDataClass.kt ├── extensionFunctionBasics │ ├── extensionFunctionBasics.iml │ └── src │ │ └── mainExtensionFunction.kt ├── functionBasics │ ├── functionBasics.iml │ └── src │ │ ├── NumberOperations.kt │ │ └── mainFunction.kt ├── helloWorld │ ├── helloWorld.iml │ └── src │ │ ├── PrintHelloWorld.kt │ │ └── mainHelloWorld.kt ├── herencia │ ├── herencia.iml │ └── src │ │ ├── A.kt │ │ ├── B.kt │ │ ├── C.kt │ │ ├── H.kt │ │ ├── W.kt │ │ ├── X.kt │ │ ├── Z.kt │ │ └── herencia.kt ├── iinterface │ ├── interface.iml │ └── src │ │ ├── A.kt │ │ ├── W.kt │ │ ├── X.kt │ │ ├── Z.kt │ │ └── interfaceMain.kt ├── infixFunctionBasics │ ├── infixFunctionBasics.iml │ └── src │ │ └── mainInfixFunctionBasics.kt ├── nullSafetyOperators │ ├── operators.iml │ └── src │ │ └── mainNullsafetyOperators.kt ├── staticValue │ ├── src │ │ ├── State.kt │ │ └── mainStaticValue.kt │ └── staticValue.iml ├── strings │ ├── src │ │ └── mainString.kt │ └── strings.iml └── variableBasics │ ├── src │ └── mainVariable.kt │ └── variableBasics.iml └── test ├── booleanOperators └── src │ └── BooleanOperatorTest.kt ├── classBasics └── src │ ├── CustomerClassTest.kt │ └── EmployeeTest.kt └── staticValue └── src └── StateTest.kt /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: vicboma1 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 13 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | GettingStartedKotlin2 -------------------------------------------------------------------------------- /.idea/GettingStartedKotlin2.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 32 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__junit_junit_4_11.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_jetbrains_kotlin_kotlin_runtime_1_0_0_beta_1038.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_jetbrains_kotlin_kotlin_stdlib_1_0_0_beta_1038.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/spek_0_1_127.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/libraries/spek_0_1_68.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/uiDesigner.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | dist: trusty 2 | language: java 3 | jdk: 4 | - openjdk11 5 | 6 | addons: 7 | apt: 8 | packages: 9 | - tree 10 | 11 | script: 12 | - mvn test -f ./pom.xml 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Getting Started Kotlin 2 | Learn the basics of getting started with kotlin 3 | 4 | [![Build](https://travis-ci.org/vicboma1/GettingStartedKotlin.svg?branch=master)](https://travis-ci.org/vicboma1/GettingStartedKotlin) [![Kotlin](http://img.shields.io/badge/Kotlin-1.3.0_rc_198-blue)](http://kotlinlang.org) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.eluder.coveralls/coveralls-maven-plugin/badge.svg)](https://maven-badges.herokuapp.com/maven-central/org.eluder.coveralls/coveralls-maven-plugin/) 5 | [![Analytics](https://ga-beacon.appspot.com/UA-68658653-5/gettingstartedkotlin/readme)](https://github.com/igrigorik/ga-beacon) 6 | 7 | --->>> [Repo: Kotlin Koans](https://github.com/vicboma1/Kotlin-Koans) <<<--- 8 | 9 | --->>> [Repo: Problems Kotlin](https://github.com/vicboma1/Kotlin-Examples-Problems/blob/master/README.md) <<<--- 10 | 11 | --->>> [Repo: GameBoy Emulator Enviroment](https://github.com/vicboma1/GameBoyEmulatorEnvironment) <<<--- 12 | 13 | --->>> [Repo: Kotlin Mobile](https://github.com/vicboma1/KotlinMobilePoC_MasterUV2018) <<<--- 14 | 15 | --->>> [Repo: Kotlin JavaScript](https://github.com/vicboma1/kotlinJavaScript) <<<--- 16 | 17 | --->>> [Repo: Kotlin Native - iOS ](https://github.com/vicboma1/Kotlin-Native-iOS) <<<--- 18 | 19 | --->>> [Repo: Ktor Examples](https://github.com/vicboma1/ktor-API-examples) <<<--- 20 | 21 | ## Indexes 22 | * [Hello World](https://github.com/vicboma1/GettingStartedKotlin#hello-world) 23 | * [Basic Variables](https://github.com/vicboma1/GettingStartedKotlin#basic-variables) 24 | * [Static Values](https://github.com/vicboma1/GettingStartedKotlin#static-values) 25 | * [Strings](https://github.com/vicboma1/GettingStartedKotlin#strings) 26 | * [Boolean Operators](https://github.com/vicboma1/GettingStartedKotlin#boolean-operators) 27 | * [Conditional](https://github.com/vicboma1/GettingStartedKotlin#conditional) 28 | * [Control Flow](https://github.com/vicboma1/GettingStartedKotlin#control-flow) 29 | * [Constructors](https://github.com/vicboma1/GettingStartedKotlin#constructors) 30 | * [Basic Functions](https://github.com/vicboma1/GettingStartedKotlin#basic-functions) 31 | * [Basic Classes](https://github.com/vicboma1/GettingStartedKotlin#basic-classes) 32 | * [Data Classes](https://github.com/vicboma1/GettingStartedKotlin#data-classes) 33 | * [Singleton Class](https://github.com/vicboma1/GettingStartedKotlin#singleton-class) 34 | * [Properties](https://github.com/vicboma1/GettingStartedKotlin#properties) 35 | * [Interface](https://github.com/vicboma1/GettingStartedKotlin#interface) 36 | * [Herencia](https://github.com/vicboma1/GettingStartedKotlin#herencia) 37 | * [Extension Function Basics](https://github.com/vicboma1/GettingStartedKotlin#extension-function-basics) 38 | * [Null Safety](https://github.com/vicboma1/GettingStartedKotlin#null-safety) 39 | * [Infix Function](https://github.com/vicboma1/GettingStartedKotlin#infix-function) 40 | 41 | 42 | ## [Hello World](https://youtu.be/iMMBrcabSOI) 43 | 44 | Main.kt 45 | ```kotlin 46 | fun main(args: Array) { 47 | val printHelloWorld = PrintHelloWorld() 48 | basic(printHelloWorld) 49 | basicEmpty(printHelloWorld) 50 | empty(printHelloWorld) 51 | } 52 | 53 | private fun empty(printHelloWorld: PrintHelloWorld) { 54 | printHelloWorld.empty() 55 | } 56 | 57 | private fun basicEmpty(printHelloWorld: PrintHelloWorld) { 58 | printHelloWorld.basicEmpty() 59 | } 60 | 61 | private fun basic(printHelloWorld: PrintHelloWorld) { 62 | val name = "Victor" 63 | printHelloWorld.basic(name) 64 | } 65 | ``` 66 | 67 | PrintHelloWorld.kt 68 | ```kotlin 69 | public class PrintHelloWorld{ 70 | companion object { val HELLO_WORLD = "Hello World " } 71 | public fun basic(name:String){ 72 | println(HELLO_WORLD+name) 73 | } 74 | 75 | public fun basicEmpty(name:String = ""){ 76 | println(HELLO_WORLD+name) 77 | } 78 | 79 | public fun empty(){ 80 | println(HELLO_WORLD) 81 | } 82 | } 83 | ``` 84 | 85 | Result 86 | ``` 87 | Hello World Victor 88 | Hello World 89 | Hello World 90 | ``` 91 | 92 | ## [Basic Variables](https://youtu.be/bklpMxuzCWU) 93 | 94 | Main.kt 95 | ```kotlin 96 | fun main(args: Array) { 97 | 98 | immutableValue() 99 | mutableValue() 100 | nullableValue() 101 | } 102 | 103 | private fun immutableValue() { 104 | val name = "Victor" 105 | //immutable prototype 106 | //val name : String = "Victor" 107 | // name = "Victor Manuel" Error! 108 | println(name) 109 | 110 | } 111 | 112 | private fun mutableValue() { 113 | //mutable prototype 114 | var lastName: String 115 | lastName = "Victor Bolinches" 116 | println(lastName) 117 | 118 | } 119 | 120 | private fun nullableValue() { 121 | //var lastNameNullable: String Error! 122 | //lastNameNullable = null Error! 123 | 124 | var lastNameNullable: String? 125 | lastNameNullable = null 126 | println(lastNameNullable) 127 | lastNameNullable = "Victor Manuel Bolinches" 128 | println(lastNameNullable) 129 | } 130 | ``` 131 | 132 | Result 133 | ``` 134 | Victor 135 | Victor Bolinches 136 | null 137 | Victor Manuel Bolinches 138 | ``` 139 | ## Static Values 140 | Main.kt 141 | ```kotlin 142 | fun main(args: Array) { 143 | start() 144 | stop() 145 | } 146 | 147 | fun start() = println("State static: ${State.START}") 148 | fun stop() = println("State static: ${State.STOP}") 149 | ``` 150 | 151 | State.kt 152 | ```kotlin 153 | public class State { 154 | companion object { 155 | val START = 1 156 | val STOP = 0 157 | } 158 | } 159 | ``` 160 | 161 | Result 162 | ``` 163 | State static = 0 164 | State static = 1 165 | ``` 166 | 167 | ## Strings 168 | 169 | Main.kt 170 | ```kotlin 171 | fun main(args: Array) { 172 | literals() 173 | templates() 174 | } 175 | 176 | private fun templates() { 177 | val number = 18 178 | val name = "vicboma $number" 179 | println(name) 180 | 181 | println("$name.length = ${name.length}") 182 | 183 | val price = "${'$'}9.99 = 9.99 dollars" 184 | println(price) 185 | } 186 | 187 | private fun literals() { 188 | val helloWorld = "Hello, world!" 189 | println(helloWorld) 190 | val str = "Hello, world!\n..." // w/backslash 191 | println(str) 192 | } 193 | ``` 194 | 195 | Result 196 | ``` 197 | Hello, world! 198 | Hello, world! 199 | ... 200 | vicboma 18 201 | vicboma 18.length = 10 202 | $9.99 = 9.99 dollars 203 | ``` 204 | 205 | ## Boolean Operators 206 | 207 | Main.kt 208 | ```kotlin 209 | fun main(args: Array) { 210 | disjunction() 211 | conjunction() 212 | negation() 213 | } 214 | 215 | private fun negation() { 216 | val list = arrayOf( 217 | BooleanOperator.negation(false), 218 | BooleanOperator.negation(true) 219 | ) 220 | 221 | printlnResult(list) 222 | } 223 | 224 | private fun conjunction() { 225 | val list = arrayOf( 226 | BooleanOperator.lazyConjunction(false, false), 227 | BooleanOperator.lazyConjunction(false, true), 228 | BooleanOperator.lazyConjunction(true, false), 229 | BooleanOperator.lazyConjunction(true, true) 230 | ) 231 | 232 | printlnResult(list) 233 | } 234 | 235 | private fun disjunction() { 236 | val list = arrayOf( 237 | BooleanOperator.lazyDisjunction(false, false), 238 | BooleanOperator.lazyDisjunction(false, true), 239 | BooleanOperator.lazyDisjunction(true, false), 240 | BooleanOperator.lazyDisjunction(true, true) 241 | ) 242 | 243 | printlnResult(list) 244 | } 245 | 246 | fun printlnResult(values:Array){ 247 | for(x in values){ 248 | println(x) 249 | } 250 | } 251 | ``` 252 | 253 | BooleanOperator.kt 254 | ```kotlin 255 | class BooleanOperator{ 256 | companion object { 257 | fun lazyDisjunction(cond1: Boolean, cond2: Boolean) = cond1 || cond2 258 | fun lazyConjunction(cond1: Boolean, cond2: Boolean) = cond1 && cond2 259 | fun negation(cond1: Boolean) = !cond1; 260 | } 261 | } 262 | ``` 263 | 264 | Result 265 | ``` 266 | //Disjunction 267 | false false = false 268 | false true = true 269 | true false = true 270 | true true = true 271 | 272 | //Conjunction 273 | false false = false 274 | false true = false 275 | true false = false 276 | true true = true 277 | 278 | //Negation 279 | false = true 280 | true = false 281 | ``` 282 | 283 | ## Conditional 284 | 285 | Main.kt 286 | ```kotlin 287 | fun main(args: Array) { 288 | val math : Math = Math() 289 | max(math) 290 | min(math) 291 | maxIn(math) 292 | minIn(math) 293 | lambdas(math) 294 | } 295 | 296 | private fun lambdas(math: Math) { 297 | val maxLambda: (Int, Int) -> Int = { x, y -> if (x > y) x else y } 298 | val minLambda: (Int, Int) -> Int = { x, y -> if (x > y) y else x } 299 | 300 | val inFunctionMax = math.inFunction(100, 3, maxLambda) 301 | val inFunctionMin = math.inFunction(100, 3, minLambda) 302 | 303 | println(inFunctionMax) 304 | println(inFunctionMin) 305 | } 306 | 307 | private fun minIn(math: Math) { 308 | val minIn = math.minIn(4, 1) 309 | val minIn1 = math.minIn(4, 4) 310 | println(minIn) 311 | println(minIn1) 312 | } 313 | 314 | private fun maxIn(math: Math) { 315 | val maxIn = math.maxIn(3, 6) 316 | println(maxIn) 317 | } 318 | 319 | private fun min(math: Math) { 320 | val min = math.min(8, 0) 321 | println(min) 322 | } 323 | 324 | private fun max(math: Math) { 325 | val max = math.max(6, 7) 326 | println(max) 327 | } 328 | 329 | ``` 330 | 331 | Math.kt 332 | ```kotlin 333 | class Math { 334 | fun max(a: Int, b: Int): Int { 335 | if (a > b) { 336 | return a 337 | } else { 338 | return b 339 | } 340 | } 341 | 342 | fun min(a: Int, b: Int): Int { 343 | if (a > b) { 344 | return b 345 | } else { 346 | return a 347 | } 348 | } 349 | 350 | fun maxIn(a: Int, b: Int): Int = if(a > b) a else b 351 | 352 | fun minIn(a: Int, b: Int): Int = if (a > b) b else a 353 | 354 | //val max : (Int, Int) -> Int = { x , y -> if (x > y) x else y } 355 | //val min : (Int, Int) -> Int = { x , y -> if (x > y) y else x } 356 | 357 | fun inFunction(x : Int, y : Int, f:(Int, Int) -> Int) : Int { 358 | return f(x,y) 359 | } 360 | } 361 | ``` 362 | 363 | Result 364 | ``` 365 | 7 366 | 0 367 | 6 368 | 1 369 | 100 370 | 3 371 | 4 372 | ``` 373 | ## Control Flow 374 | 375 | #### While 376 | Main.kt 377 | ```kotlin 378 | fun main(args: Array) { 379 | ArrayWithIterator() 380 | ArrayDoWhile() 381 | classic() 382 | } 383 | 384 | private fun ArrayDoWhile() { 385 | var arraySafety = arrayOf(1, 2, 3, 4, 5, null) 386 | doWhile(arraySafety) 387 | } 388 | 389 | private fun ArrayWithIterator() { 390 | var arrayAny = arrayOf(1, 2, 3, 4, 5) 391 | withIterator(arrayAny) 392 | } 393 | 394 | private fun classic() { 395 | var i = 5 396 | while (0 <= i) { 397 | println(i--) 398 | } 399 | } 400 | 401 | private fun doWhile(arraySafety: Array) { 402 | val iterator = arraySafety.iterator() 403 | do { 404 | val y = iterator.next() 405 | println(y) 406 | } while (y != null) // y is visible here! 407 | } 408 | 409 | private fun withIterator(arrayAny: Array) { 410 | val iterator = arrayAny.iterator() 411 | while (iterator.hasNext()) { 412 | val next = iterator.next() 413 | println(next) 414 | } 415 | } 416 | ``` 417 | 418 | Result 419 | ``` 420 | 1 421 | 2 422 | 3 423 | 4 424 | 5 425 | 426 | 1 427 | 2 428 | 3 429 | 4 430 | 5 431 | null 432 | 433 | 5 434 | 4 435 | 3 436 | 2 437 | 1 438 | 0 439 | ``` 440 | 441 | #### For 442 | Main.kt 443 | ```kotlin 444 | fun main(args: Array) { 445 | 446 | var arrayAny = arrayOf(12,2.3,45F,"Soy una String",true, null) 447 | anIterator(arrayAny) 448 | withBodyBlock(arrayAny) 449 | withIndices(arrayAny) 450 | } 451 | 452 | private fun withIndices(arrayAny: Array) { 453 | for (i in arrayAny.indices) 454 | println(arrayAny[i]) 455 | } 456 | 457 | private fun anIterator(arrayAny: Array) { 458 | for (any in arrayAny) 459 | println(any) 460 | } 461 | 462 | private fun withBodyBlock(arrayAny: Array) { 463 | for (any: Any? in arrayAny) { 464 | print(any) 465 | print("\n") 466 | } 467 | } 468 | ``` 469 | Result 470 | ``` 471 | 12 472 | 2.3 473 | 45.0 474 | Soy una String 475 | true 476 | null 477 | 478 | 12 479 | 2.3 480 | 45.0 481 | Soy una String 482 | true 483 | null 484 | 485 | 12 486 | 2.3 487 | 45.0 488 | Soy una String 489 | true 490 | null 491 | ``` 492 | 493 | 494 | #### When 495 | 496 | Main.kt 497 | ```kotlin 498 | fun main(args: Array) { 499 | var array = arrayOf(1,2,3) 500 | forWhenDefault(array) 501 | forWhenCombined(array) 502 | forImplicitCast() 503 | expression() 504 | } 505 | 506 | private fun expression() { 507 | var arrayNumber = arrayOf(1, 2, 19, 20, 14, 35, 45) 508 | expression(arrayNumber) 509 | } 510 | 511 | private fun forImplicitCast() { 512 | var arrayAny = arrayOf(1, 2.0, 5F, "", true) 513 | implicitCasts(arrayAny) 514 | } 515 | 516 | private fun forWhenCombined(array: Array) { 517 | for (i in array) 518 | whenCombined(i) 519 | } 520 | 521 | private fun forWhenDefault(array: Array) { 522 | for (i in array) 523 | _whenDefault(i) 524 | } 525 | 526 | /** 527 | * We can also check a value For being in or !in a range or a collection: 528 | */ 529 | private fun expression(arrayNumber: Array) { 530 | val validNumbers = arrayOf(35) 531 | for (obj in arrayNumber) 532 | when (obj) { 533 | in 1..10 -> println("x is in the range") 534 | in validNumbers -> println("x is valid") 535 | !in 10..20 -> println("x is outside the range") 536 | else -> println("none of the above") 537 | } 538 | } 539 | 540 | /** 541 | * with patter matching 542 | */ 543 | private fun implicitCasts(arrayAny: Array) { 544 | for (obj in arrayAny) 545 | when (obj) { 546 | is String -> println("is String") 547 | is Int -> println("is Int") 548 | is Float -> println("is Float") 549 | is Double -> println("is Double") 550 | !is Boolean -> println("is not Boolean") 551 | else -> println("is Boolean ") 552 | } 553 | } 554 | 555 | 556 | /** 557 | * If many cases should be handled in the same way, the branch conditions may be combined with a comma 558 | */ 559 | private fun whenCombined(i: Int) { 560 | when (i) { 561 | 0, 1 -> println("x == 0 or x == 1") 562 | else -> println("otherwise") 563 | } 564 | } 565 | 566 | /** 567 | * when replaces the switch operator of C-like languages 568 | * when can be used either as an expression or as a statement 569 | */ 570 | private fun _whenDefault(x: Int) { 571 | when (x) { 572 | 1 -> println("x == 1") 573 | 2 -> println("x == 2") 574 | else -> { 575 | // Note the block 576 | println("x is neither 1 nor 2") 577 | } 578 | } 579 | } 580 | ``` 581 | 582 | Result 583 | ``` 584 | x == 1 585 | x == 2 586 | x is neither 1 nor 587 | 588 | x == 0 or x == 1 589 | otherwise 590 | otherwise 591 | 592 | is Int 593 | is Double 594 | is Float 595 | is String 596 | is Boolean 597 | 598 | x is in the range 599 | x is in the range 600 | none of the above 601 | none of the above 602 | none of the above 603 | x is valid 604 | x is outside the range 605 | 606 | ``` 607 | 608 | #### Return and Jump 609 | Kotlin has three structural jump operators 610 | * - return. By default returns from the nearest enclosing function or function expression. 611 | * — break. Terminates the nearest enclosing loop. 612 | * - continue. Proceeds to the next step of the nearest enclosing loop. 613 | 614 | ```kotlin 615 | fun main(args: Array) { 616 | returnBasic() 617 | inlineReturn() 618 | implicitReturn() 619 | breakLoopContinue() 620 | } 621 | 622 | private fun implicitReturn() { 623 | println("Init implicit return") 624 | listOf(1, 2, 3, 4, 5).forEach { 625 | if (it == 2) { 626 | println("Exit implicit return") 627 | return@forEach 628 | } 629 | println(it) 630 | } 631 | } 632 | 633 | private fun inlineReturn() { 634 | 635 | println("Init inline return") 636 | listOf(1, 2, 3, 4, 5).forEach lit@ { 637 | if (it == 5) { 638 | println("Exit inline return") 639 | return@lit 640 | } 641 | println(it) 642 | } 643 | } 644 | 645 | private fun returnBasic(): Unit { 646 | println("Init Basic return") 647 | for (i in 0..5) { 648 | if (i == 5) { 649 | println("Exit Basic return") 650 | return 651 | } 652 | println(i) 653 | } 654 | 655 | } 656 | 657 | private fun breakLoopContinue() { 658 | println("Init Basic Loop") 659 | Continue@ for (i in 1..100) { 660 | for (j in 1..100) { 661 | if (j === 50) { 662 | break@Continue 663 | } 664 | } 665 | } 666 | println("Exit Basic Loop") 667 | } 668 | ``` 669 | 670 | Result 671 | ``` 672 | //returnBasic 673 | Init Basic return 674 | 0 675 | 1 676 | 2 677 | 3 678 | 4 679 | Exit Basic return 680 | 681 | //inlineReturn 682 | Init inline return 683 | 1 684 | 2 685 | 3 686 | 4 687 | Exit inline return 688 | 689 | //implicitReturn 690 | Init implicit return 691 | 1 692 | Exit implicit return 693 | 3 694 | 4 695 | 5 696 | 697 | //breakLoopContinue 698 | Init breakLoopContinue Loop 699 | Exit breakLoopContinue Loop 700 | ``` 701 | ## Constructors 702 | 703 | Primary 704 | ```kotlin 705 | class MyView (ctx: Context): View(ctx) { 706 | ... 707 | } 708 | ``` 709 | 710 | Secundary 711 | ```kotlin 712 | class MyView : View { 713 | constructor(ctx: Context) : super(ctx) 714 | constructor(ctx: Context, attrs: AttributeSet) : super(ctx, attrs) 715 | } 716 | ``` 717 | 718 | Internal 719 | ```kotlin 720 | class MyView internal constructor(ctx: Context) : View (ctx) { 721 | ... 722 | } 723 | ``` 724 | 725 | ## [Basics Functions ](https://youtu.be/-4XuESEe45c) 726 | 727 | Main.kt 728 | ```kotlin 729 | fun main(args: Array) { 730 | val numberOperations = NumberOperations() 731 | 732 | twoNumbers(numberOperations) 733 | twoNumberReverse(numberOperations) 734 | addTwoNumbersDefault(numberOperations) 735 | reverseList(numberOperations) 736 | reverseList2(numberOperations) 737 | sumLambda(numberOperations) 738 | sumLambdainFunction(numberOperations) 739 | sumInlineOperation(numberOperations) 740 | } 741 | 742 | private fun sumInlineOperation(numberOperations: NumberOperations) { 743 | val sumInFunction = numberOperations.sumInFunction(5, 9, { x, y -> x + y }) 744 | println(sumInFunction) 745 | } 746 | 747 | private fun sumLambdainFunction(numberOperations: NumberOperations) { 748 | val _sumLambda: (Int, Int) -> Int = { x, y -> x + y } 749 | val _sumInFunction = numberOperations.sumInFunction(9, 9, _sumLambda) 750 | println(_sumInFunction) 751 | } 752 | 753 | private fun sumLambda(numberOperations: NumberOperations) { 754 | val sumLambda = numberOperations.sumLambda(2, 2) 755 | println(sumLambda) 756 | } 757 | 758 | private fun reverseList2(numberOperations: NumberOperations) { 759 | val reverseList1 = numberOperations.reverseList(listOf(1, 2, 3, 4)).asList() 760 | println(reverseList1) 761 | } 762 | 763 | private fun reverseList(numberOperations: NumberOperations) { 764 | val reverseList = numberOperations.reverseList(arrayListOf(1, 2, 3)).asList() 765 | println(reverseList) 766 | } 767 | 768 | private fun addTwoNumbersDefault(numberOperations: NumberOperations) { 769 | val addTwoNumbersDefault = numberOperations.addTwoNumbersDefault(2) 770 | println(addTwoNumbersDefault) 771 | } 772 | 773 | private fun twoNumbers(numberOperations: NumberOperations) { 774 | val addTwoNumbers = numberOperations.addTwoNumbers(first = 2, second = 5) 775 | println(addTwoNumbers) 776 | } 777 | 778 | private fun twoNumberReverse(numberOperations: NumberOperations) { 779 | val addTwoNumbers1 = numberOperations.addTwoNumbers(second = 5, first = 2) 780 | println(addTwoNumbers1) 781 | } 782 | ``` 783 | 784 | NumberOperations.kt 785 | ```kotlin 786 | public class NumberOperations{ 787 | 788 | fun addTwoNumbers(first:Int, second :Int) : Int { 789 | val result = first + second 790 | return result 791 | } 792 | 793 | fun addTwoNumbersDefault(first:Int, second :Int = 0) : Int { 794 | val result = first + second 795 | return result 796 | } 797 | 798 | inline fun reverseList(list : List) : Array { 799 | return list.reversed().toTypedArray(); 800 | } 801 | 802 | Funciones de Orden Superior y Expresiones Lambda (como en Haskell :D) 803 | 804 | val sumLambda : (Int, Int) -> Int = { x,y -> x + y} 805 | 806 | fun sumInFunction(x : Int, y : Int, f:(Int, Int) -> Int) : Int { 807 | return f(x,y) 808 | } 809 | 810 | } 811 | ``` 812 | 813 | Result 814 | ``` 815 | 5 + 2 = 7 816 | 2 + 5 = 7 817 | 2 + 0 = 2 818 | [1, 2, 3] = [3, 2, 1] 819 | [1, 2, 3, 4] = [4, 3, 2, 1] 820 | 2 + 2 = 4 821 | 9 + 9 = 18 822 | 5 + 9 = 14 823 | ``` 824 | 825 | ## [Basic Classes](https://youtu.be/SI2HiSLDFFs) 826 | 827 | Main.kt 828 | ```kotlin 829 | fun main(args: Array) { 830 | customerMethod() 831 | employeeMethod() 832 | } 833 | 834 | private fun employeeMethod() { 835 | val employee = Employee(1221, "Vicboma") 836 | val toString = employee.toString() 837 | val id = employee.id 838 | val name = employee.name 839 | 840 | println("Id : $id Name: $name") 841 | println("Employee ToString: $toString") 842 | println(employee) 843 | } 844 | 845 | private fun customerMethod() { 846 | val customer = Customer(1234) 847 | customer.doSomething(); 848 | val toString = customer.toString(); 849 | val id = customer.id; 850 | println("Customer id: $id"); 851 | println("Customer ToString: $toString"); 852 | } 853 | ``` 854 | 855 | Customer.kt 856 | ```kotlin 857 | public class Customer(_id : Int) { 858 | 859 | val id = _id; 860 | //prototype 861 | var name : String? = "" 862 | 863 | fun doSomething(){ 864 | println("Some code") 865 | } 866 | 867 | override fun toString() : String 868 | { 869 | return ""+this.id; 870 | } 871 | } 872 | ``` 873 | 874 | Employee.kt 875 | ```kotlin 876 | public class Employee( val id : Int, val name: String ) { 877 | } 878 | ``` 879 | 880 | Result 881 | ``` 882 | Some code 883 | Customer id: 1234 884 | Customer ToString: 1234 885 | Id : 1221 Name: Vicboma 886 | Employee ToString: Employee@4cc77c2e 887 | ``` 888 | 889 | ## [Data Classes](https://youtu.be/GhwZNoQqejQ) 890 | 891 | Main.kt 892 | ```kotlin 893 | fun main(args: Array) { 894 | templateExamples() 895 | } 896 | 897 | private fun templateExamples() { 898 | val customer1 = Customer(1, "victor", "victorbolinchesmarin@gmail.com") 899 | val customer2 = Customer(1, "victor", "victorbolinchesmarin@gmail.com") 900 | val customer3 = customer1; 901 | val dataCustomer4 = DataCustomer(1, "victor", "victorbolinchesmarin@gmail.com") 902 | val dataCustomer5 = dataCustomer4; 903 | 904 | println(customer1) //toString 905 | println(customer1.hashCode()) //hashCode 906 | println(customer1.equals(customer2)) //equals() 907 | println(customer1.equals(customer3)) 908 | 909 | //The compiler automatically derives the following members from all properties declared in the primary constructor: 910 | //equals()/hashCode() pair, 911 | //toString() of the form "User(name=John, age=42)", 912 | //componentN() functions corresponding to the properties in their order or declaration, 913 | //copy() function (see below). 914 | println(dataCustomer4.equals(dataCustomer5)) 915 | println(dataCustomer4) 916 | } 917 | ``` 918 | 919 | Customer.kt 920 | ```kotlin 921 | public class Customer(val id : Int, val name : String, val email : String) { 922 | } 923 | ``` 924 | 925 | DataCustomer.kt 926 | ```kotlin 927 | /** 928 | * The primary constructor needs to have at least one parameter; 929 | * All primary constructor parameters need to be marked as val or var; 930 | * Data classes cannot be abstract, open, sealed or inner; 931 | * Data classes may not extend other classes (but may implement interfaces). 932 | */ 933 | data public class DataCustomer(val id : Int, val name : String, val email : String) { 934 | } 935 | ``` 936 | 937 | Result 938 | ``` 939 | Customer@4cc77c2e 940 | 1288141870 941 | false 942 | true 943 | true 944 | DataCustomer(id=1, name=victor, email=victorbolinchesmarin@gmail.com) 945 | ``` 946 | 947 | ## Singleton Class 948 | 949 | ```java 950 | public class Singleton { 951 | 952 | private String b; 953 | private static Singleton instance; 954 | 955 | private Singleton() { 956 | System.out.println("This "+this+" is a singleton") 957 | } 958 | 959 | public static Singleton getInstance() { 960 | if (instance == null) 961 | instance = new Singleton(); 962 | 963 | return instance; 964 | } 965 | 966 | public void setB(String b) { this.b = b;} 967 | public String getB() { return this.b; } 968 | } 969 | 970 | --------- 971 | final Singleton first = new Singleton() 972 | first.setB("hello singleton"= 973 | 974 | String second = new Singleton() 975 | println(second.getB()) 976 | 977 | ``` 978 | 979 | ```kotlin 980 | object Singleton { 981 | init { 982 | println("This ($this) is a singleton") 983 | } 984 | 985 | var b:String? = null 986 | } 987 | 988 | --------- 989 | 990 | var first = Singleton 991 | first.b = "hello singleton" 992 | 993 | var second = Singleton 994 | println(second.b) 995 | 996 | ``` 997 | 998 | Result 999 | ``` 1000 | This (Singobj@728938a9) is a singleton 1001 | hello singleton 1002 | ``` 1003 | 1004 | ## Properties 1005 | ```kotlin 1006 | class Rectangle(val height: Int, val width: Int) { 1007 | val isSquare: Boolean 1008 | get() = (height == width) 1009 | set(value) 1010 | field = value 1011 | } 1012 | 1013 | ``` 1014 | 1015 | ## Interface 1016 | Main.kt 1017 | ```kotlin 1018 | fun main(args: Array) { 1019 | val z = Z("I am Z") 1020 | val zToString = z.toString() 1021 | println(zToString) 1022 | val zFizz = z.fiz() 1023 | println(zFizz) 1024 | val zBuzz = z.buzz() 1025 | println(zBuzz) 1026 | } 1027 | ``` 1028 | X.kt 1029 | ```kotlin 1030 | interface X{ 1031 | fun fiz() : String 1032 | } 1033 | ``` 1034 | 1035 | A.kt 1036 | ```kotlin 1037 | interface A{ 1038 | fun buzz() :String 1039 | } 1040 | ``` 1041 | 1042 | Z.kt 1043 | ```kotlin 1044 | open class Z(val arg:String) : A, X { 1045 | override fun toString() = "ToString() : $arg" 1046 | override fun fiz() : String = "Z - fiz" 1047 | override fun buzz() : String = "Z - buzz" 1048 | } 1049 | ``` 1050 | 1051 | Result 1052 | ``` 1053 | ToString() : I am Z 1054 | Z - fiz 1055 | Z - buzz 1056 | ``` 1057 | 1058 | 1059 | ## Herencia 1060 | Main.kt 1061 | ```kotlin 1062 | fun main(args: Array) { 1063 | val b = B("I am B") 1064 | val bToString = b.toString() 1065 | println(bToString) 1066 | val bFizz = b.fiz() 1067 | println(bFizz) 1068 | val bBuzz = b.buzz() 1069 | println(bBuzz) 1070 | 1071 | val bValue = b.fizBuzz 1072 | println("b.class.toString() $bValue") 1073 | 1074 | val c = C() 1075 | val cToString = c.toString() 1076 | println(cToString) 1077 | val cFizz = c.fiz() 1078 | println(cFizz) 1079 | val cBuzz = c.buzz() 1080 | println(cBuzz) 1081 | val cValue = c.fizBuzz 1082 | println("c.class.toString() $cValue") 1083 | 1084 | val h = H("I am H") 1085 | val hToString = h.toString() 1086 | println(cToString) 1087 | val hFizz = h.fiz() 1088 | println(hFizz) 1089 | val hBuzz = h.buzz() 1090 | println(hBuzz) 1091 | } 1092 | ``` 1093 | 1094 | X.kt 1095 | ```kotlin 1096 | interface X{ 1097 | fun fiz() : String 1098 | } 1099 | ``` 1100 | 1101 | A.kt 1102 | ```kotlin 1103 | interface A{ 1104 | fun buzz() :String 1105 | } 1106 | ``` 1107 | 1108 | Z.kt 1109 | ```kotlin 1110 | open class Z(val arg:String) : A, X { 1111 | override fun toString() = "ToString() : $arg" 1112 | override fun fiz() : String = "Z - fiz" 1113 | override fun buzz() : String = "Z - buzz" 1114 | } 1115 | ``` 1116 | 1117 | B.kt 1118 | ```kotlin 1119 | class B(val name:String) : Z("Z"), W { 1120 | override val fizBuzz : Int = 29 1121 | override fun fiz() : String = "B - fiz" 1122 | override fun buzz() : String = "B - buzz" 1123 | override fun toString() : String = "ToString() : ${this.name} and my parent is ${super.toString()}" 1124 | } 1125 | ``` 1126 | 1127 | C.kt 1128 | ```kotlin 1129 | class C : Z("I am Z"), W { 1130 | override val fizBuzz : Int = 92 1131 | override fun fiz() : String = "C - fiz" 1132 | override fun buzz() : String = "C - buzz" 1133 | } 1134 | ``` 1135 | 1136 | H.kt 1137 | ```kotlin 1138 | class H(val name:String):Z("I am Z") { 1139 | override fun fiz() :String = super.fiz() 1140 | override fun buzz() : String = super.buzz() 1141 | } 1142 | ``` 1143 | 1144 | Result 1145 | ``` 1146 | ToString() : I am B and my parent is ToString() : Z 1147 | B - fiz 1148 | B - buzz 1149 | b.class.toString() 29 1150 | ToString() : I am Z 1151 | C - fiz 1152 | C - buzz 1153 | c.class.toString() 92 1154 | ToString() : I am Z 1155 | Z - fiz 1156 | Z - buzz 1157 | ``` 1158 | 1159 | ## [Extension Function Basics](https://youtu.be/SMzNduaGcuo) 1160 | 1161 | Main.kt 1162 | ```kotlin 1163 | fun main(args: Array) { 1164 | val str = functionClassic() 1165 | functionInvokeInstance(str) 1166 | } 1167 | 1168 | private fun functionInvokeInstance(str: String) { 1169 | val convertSpacesToUnderscoresInvokeInstanceString = str.convertSpacesToUnderscoresInvokeInstanceString() 1170 | println(convertSpacesToUnderscoresInvokeInstanceString) 1171 | } 1172 | 1173 | private fun functionClassic(): String { 1174 | val str = "this is my text" 1175 | val convertSpacesToUnderscores = convertSpacesToUnderscores(str); 1176 | println(convertSpacesToUnderscores) 1177 | return str 1178 | } 1179 | 1180 | 1181 | fun convertSpacesToUnderscores(str: String) : String { 1182 | return str.replace(" ","_") 1183 | } 1184 | 1185 | fun String.convertSpacesToUnderscoresInvokeInstanceString() : String { 1186 | return this.replace(" ","_") 1187 | } 1188 | ``` 1189 | 1190 | Result 1191 | ``` 1192 | function: this_is_my_text 1193 | Invoke Instance String: this_is_my_text 1194 | ``` 1195 | 1196 | ## Null Safety 1197 | 1198 | Main.kt 1199 | ```kotlin 1200 | fun main(args: Array) { 1201 | checkingForNullcondition() 1202 | val listMutable = safetyAccess() 1203 | elvisOperator(listMutable) 1204 | safetyCasts() 1205 | specialOperator() 1206 | } 1207 | 1208 | private fun specialOperator() { 1209 | val str: String? = null 1210 | val len = str!!.length //accedemos a la propiedad, pero dará una excepción. 1211 | } 1212 | 1213 | private fun safetyCasts() { 1214 | val a: Double = 2.0 1215 | val aInt: Int? = a as? Int 1216 | println(aInt) 1217 | } 1218 | 1219 | private fun elvisOperator(listMutable: Array) { 1220 | val result = listMutable?.size ?: -1 1221 | println(result) 1222 | } 1223 | 1224 | //Safe Calls - Any"?".property 1225 | private fun safetyAccess(): Array { 1226 | val listMutable = arrayOf(1, 2, 3, 4) 1227 | 1228 | val _size = listMutable?.size 1229 | if (listMutable != null && _size > 0) 1230 | println("Array of size $_size") 1231 | else 1232 | println("Empty Array") 1233 | return listMutable 1234 | } 1235 | 1236 | private fun checkingForNullcondition() { 1237 | val listImmutable = listOf(1, 2, 3, 4) 1238 | 1239 | val size = listImmutable.size 1240 | if (listImmutable != null && size > 0) 1241 | println("Array of size $size") 1242 | else 1243 | println("Empty Array") 1244 | } 1245 | ``` 1246 | 1247 | Result 1248 | ``` 1249 | Array of size 4 1250 | Array of size 4 1251 | 4 1252 | null 1253 | Exception in thread "main" kotlin.KotlinNullPointerException 1254 | at MainNullsafetyOperatorsKt.main(mainNullsafetyOperators.kt:39) 1255 | ``` 1256 | 1257 | ## Infix Function 1258 | 1259 | Main.kt 1260 | ```kotlin 1261 | fun main(args: Array) { 1262 | val list = listOf(10, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5) 1263 | 1264 | doubleResult(list) 1265 | filter(list) 1266 | map(list) 1267 | } 1268 | //infix without paren + brace 1269 | private fun map(list: List) = list.filter{ it % 2 == 0 }.map{ it - 1 }.forEach{ println(it) } 1270 | 1271 | //infix without Dots & paren + brace 1272 | private fun filter(list: List) = list filter { it % 2 == 0 } forEach { println(it) } 1273 | 1274 | //infix with paren & brace 1275 | private fun doubleResult(list: List) = list.forEach({ println(it * 2) }) 1276 | 1277 | ``` 1278 | 1279 | Result 1280 | ``` 1281 | 20 1282 | -10 1283 | -8 1284 | -6 1285 | -4 1286 | -2 1287 | 0 1288 | 2 1289 | 4 1290 | 6 1291 | 8 1292 | 10 1293 | 1294 | 10 1295 | -4 1296 | -2 1297 | 0 1298 | 2 1299 | 4 1300 | 1301 | 9 1302 | -5 1303 | -3 1304 | -1 1305 | 1 1306 | 3 1307 | ``` 1308 | 1309 | Reference: 1310 | * Main : http://kotlin.es 1311 | * Facebook : https://www.facebook.com/kotlin.es 1312 | * Video tutorials: https://kotlinlang.org/docs/videos.html 1313 | * Manual: https://kotlinlang.org/docs/kotlin-docs.pdf 1314 | * @Author: Victor Bolinches Marin 1315 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | BasicsKotlin 8 | GettingStartedKotlin 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 1.8 13 | 1.3.0-rc-198 14 | 4.0.0 15 | ${maven.build.timestamp} 16 | UTF-8 17 | 18 | 19 | 20 | 21 | org.jetbrains.kotlin 22 | kotlin-stdlib 23 | ${kotlin.version} 24 | 25 | 26 | 27 | junit 28 | junit 29 | 4.13.1 30 | test 31 | 32 | 33 | org.jetbrains.kotlin 34 | kotlin-test 35 | RELEASE 36 | test 37 | 38 | 45 | 46 | 47 | 48 | jebrains-all 49 | http://repository.jetbrains.com/all 50 | 51 | 52 | 53 | ${project.basedir}/src/main/test 54 | 55 | 56 | org.apache.maven.plugins 57 | maven-surefire-plugin 58 | 2.19.1 59 | 60 | true 61 | 62 | 63 | 64 | kotlin-maven-plugin 65 | org.jetbrains.kotlin 66 | ${kotlin.version} 67 | 68 | 69 | compile 70 | compile 71 | 72 | compile 73 | 74 | 75 | 76 | test-compile 77 | test-compile 78 | 79 | test-compile 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /src/main/java/booleanOperators/classBooleanOperators.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/main/java/booleanOperators/src/BooleanOperator.kt: -------------------------------------------------------------------------------- 1 | package booleanOperators.src 2 | 3 | /** 4 | * Created by vicboma on 28/10/15. 5 | */ 6 | class BooleanOperator{ 7 | companion object { 8 | fun lazyDisjunction(cond1: Boolean, cond2: Boolean) = cond1 || cond2 9 | fun lazyConjunction(cond1: Boolean, cond2: Boolean) = cond1 && cond2 10 | fun negation(cond1: Boolean) = !cond1; 11 | } 12 | } -------------------------------------------------------------------------------- /src/main/java/booleanOperators/src/mainBooleanOperators.kt: -------------------------------------------------------------------------------- 1 | import booleanOperators.src.BooleanOperator 2 | 3 | /** 4 | * Created by vicboma on 27/10/15. 5 | */ 6 | fun main(args: Array) { 7 | disjunction() 8 | conjunction() 9 | negation() 10 | } 11 | 12 | private fun negation() { 13 | val list = arrayOf( 14 | BooleanOperator.negation(false), 15 | BooleanOperator.negation(true) 16 | ) 17 | 18 | printlnResult(list) 19 | } 20 | 21 | private fun conjunction() { 22 | val list = arrayOf( 23 | BooleanOperator.lazyConjunction(false, false), 24 | BooleanOperator.lazyConjunction(false, true), 25 | BooleanOperator.lazyConjunction(true, false), 26 | BooleanOperator.lazyConjunction(true, true) 27 | ) 28 | 29 | printlnResult(list) 30 | } 31 | 32 | private fun disjunction() { 33 | val list = arrayOf( 34 | BooleanOperator.lazyDisjunction(false, false), 35 | BooleanOperator.lazyDisjunction(false, true), 36 | BooleanOperator.lazyDisjunction(true, false), 37 | BooleanOperator.lazyDisjunction(true, true) 38 | ) 39 | 40 | printlnResult(list) 41 | } 42 | 43 | fun printlnResult(values:Array){ 44 | for(x in values){ 45 | println(x) 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/classBasics/classBasics.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/main/java/classBasics/src/CustomerClass.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by vicboma on 27/10/15. 3 | */ 4 | public class CustomerClass(_id : Int) { 5 | val id = _id; 6 | //prototype 7 | var name : String? = "" 8 | 9 | fun doSomething() : String { 10 | return "Some code" 11 | } 12 | 13 | override fun toString() : String 14 | { 15 | return ""+this.id 16 | } 17 | } -------------------------------------------------------------------------------- /src/main/java/classBasics/src/Employee.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by vicboma on 27/10/15. 3 | */ 4 | public class Employee( val id : Int, val name: String ) { 5 | } -------------------------------------------------------------------------------- /src/main/java/classBasics/src/mainClass.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by vicboma on 27/10/15. 3 | */ 4 | fun main(args: Array) { 5 | customerMethod() 6 | employeeMethod() 7 | } 8 | 9 | private fun employeeMethod() { 10 | val employee = Employee(1221, "Vicboma") 11 | val toString = employee.toString() 12 | val id = employee.id 13 | val name = employee.name 14 | 15 | println("Id : $id Name: $name") 16 | println("Employee ToString: $toString") 17 | } 18 | 19 | private fun customerMethod() { 20 | val customer = CustomerClass(1234) 21 | customer.doSomething(); 22 | val toString = customer.toString(); 23 | val id = customer.id; 24 | println("Customer id: $id"); 25 | println("Customer ToString: $toString"); 26 | } -------------------------------------------------------------------------------- /src/main/java/conditionals/Conditionals.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /src/main/java/conditionals/src/Math.kt: -------------------------------------------------------------------------------- 1 | package classBasics.src 2 | 3 | /** 4 | * Created by vicboma on 28/10/15. 5 | */ 6 | class Math { 7 | 8 | fun max(a: Int, b: Int): Int { 9 | if (a > b) { 10 | return a 11 | } else { 12 | return b 13 | } 14 | } 15 | 16 | fun min(a: Int, b: Int): Int { 17 | if (a > b) { 18 | return b 19 | } else { 20 | return a 21 | } 22 | } 23 | 24 | fun maxIn(a: Int, b: Int): Int = if(a > b) a else b 25 | 26 | fun minIn(a: Int, b: Int): Int = if (a > b) b else a 27 | 28 | //val max : (Int, Int) -> Int = { x , y -> if (x > y) x else y } 29 | //val min : (Int, Int) -> Int = { x , y -> if (x > y) y else x } 30 | 31 | fun inFunction(x : Int, y : Int, f:(Int, Int) -> Int) : Int { 32 | return f(x,y) 33 | } 34 | } -------------------------------------------------------------------------------- /src/main/java/conditionals/src/mainConditionals.kt: -------------------------------------------------------------------------------- 1 | import classBasics.src.Math 2 | /** 3 | * Created by vicboma on 27/10/15. 4 | */ 5 | fun main(args: Array) { 6 | val math : Math = Math() 7 | max(math) 8 | min(math) 9 | maxIn(math) 10 | minIn(math) 11 | lambdas(math) 12 | } 13 | 14 | private fun lambdas(math: Math) { 15 | val maxLambda: (Int, Int) -> Int = { x, y -> if (x > y) x else y } 16 | val minLambda: (Int, Int) -> Int = { x, y -> if (x > y) y else x } 17 | 18 | val inFunctionMax = math.inFunction(100, 3, maxLambda) 19 | val inFunctionMin = math.inFunction(100, 3, minLambda) 20 | 21 | println(inFunctionMax) 22 | println(inFunctionMin) 23 | } 24 | 25 | private fun minIn(math: Math) { 26 | val minIn = math.minIn(4, 1) 27 | val minIn1 = math.minIn(4, 4) 28 | println(minIn) 29 | println(minIn1) 30 | } 31 | 32 | private fun maxIn(math: Math) { 33 | val maxIn = math.maxIn(3, 6) 34 | println(maxIn) 35 | } 36 | 37 | private fun min(math: Math) { 38 | val min = math.min(8, 0) 39 | println(min) 40 | } 41 | 42 | private fun max(math: Math) { 43 | val max = math.max(6, 7) 44 | println(max) 45 | } 46 | 47 | -------------------------------------------------------------------------------- /src/main/java/controlFlow/_For/main_For.kt: -------------------------------------------------------------------------------- 1 | package controlFlow._For 2 | 3 | /** 4 | * Created by vicboma on 28/10/15. 5 | */ 6 | fun main(args: Array) { 7 | var arrayAny = arrayOf(12,2.3,45F,"Soy una String",true, null) 8 | anIterator(arrayAny) 9 | withBodyBlock(arrayAny) 10 | withIndices(arrayAny) 11 | } 12 | 13 | private fun withIndices(arrayAny: Array) { 14 | for (i in arrayAny.indices) 15 | println(arrayAny[i]) 16 | } 17 | 18 | private fun anIterator(arrayAny: Array) { 19 | for (any in arrayAny) 20 | println(any) 21 | } 22 | 23 | private fun withBodyBlock(arrayAny: Array) { 24 | for (any: Any? in arrayAny) { 25 | print(any) 26 | print("\n") 27 | } 28 | } -------------------------------------------------------------------------------- /src/main/java/controlFlow/_when/mainWhen.kt: -------------------------------------------------------------------------------- 1 | package controlFlow._when 2 | /** 3 | * Created by vicboma on 27/10/15. 4 | */ 5 | fun main(args: Array) { 6 | var array = arrayOf(1,2,3) 7 | forWhenDefault(array) 8 | forWhenCombined(array) 9 | forImplicitCast() 10 | expression() 11 | } 12 | 13 | private fun expression() { 14 | var arrayNumber = arrayOf(1, 2, 19, 20, 14, 35, 45) 15 | expression(arrayNumber) 16 | } 17 | 18 | private fun forImplicitCast() { 19 | var arrayAny = arrayOf(1, 2.0, 5F, "", true) 20 | implicitCasts(arrayAny) 21 | } 22 | 23 | private fun forWhenCombined(array: Array) { 24 | for (i in array) 25 | whenCombined(i) 26 | } 27 | 28 | private fun forWhenDefault(array: Array) { 29 | for (i in array) 30 | _whenDefault(i) 31 | } 32 | 33 | /** 34 | * We can also check a value For being in or !in a range or a collection: 35 | */ 36 | private fun expression(arrayNumber: Array) { 37 | val validNumbers = arrayOf(35) 38 | for (obj in arrayNumber) 39 | when (obj) { 40 | in 1..10 -> println("x is in the range") 41 | in validNumbers -> println("x is valid") 42 | !in 10..20 -> println("x is outside the range") 43 | else -> println("none of the above") 44 | } 45 | } 46 | 47 | /** 48 | * with patter matching 49 | */ 50 | private fun implicitCasts(arrayAny: Array) { 51 | for (obj in arrayAny) 52 | when (obj) { 53 | is String -> println("is String") 54 | is Int -> println("is Int") 55 | is Float -> println("is Float") 56 | is Double -> println("is Double") 57 | !is Boolean -> println("is not Boolean") 58 | else -> println("is Boolean ") 59 | } 60 | } 61 | 62 | 63 | /** 64 | * If many cases should be handled in the same way, the branch conditions may be combined with a comma 65 | */ 66 | private fun whenCombined(i: Int) { 67 | when (i) { 68 | 0, 1 -> println("x == 0 or x == 1") 69 | else -> println("otherwise") 70 | } 71 | } 72 | 73 | /** 74 | * when replaces the switch operator of C-like languages 75 | * when can be used either as an expression or as a statement 76 | */ 77 | private fun _whenDefault(x: Int) { 78 | when (x) { 79 | 1 -> println("x == 1") 80 | 2 -> println("x == 2") 81 | else -> { 82 | // Note the block 83 | println("x is neither 1 nor 2") 84 | } 85 | } 86 | } -------------------------------------------------------------------------------- /src/main/java/controlFlow/_while/mainWhile.kt: -------------------------------------------------------------------------------- 1 | package controlFlow._while 2 | 3 | /** 4 | * Created by vicboma on 28/10/15. 5 | */ 6 | fun main(args: Array) { 7 | ArrayWithIterator() 8 | ArrayDoWhile() 9 | classic() 10 | } 11 | 12 | private fun ArrayDoWhile() { 13 | var arraySafety = arrayOf(1, 2, 3, 4, 5, null) 14 | doWhile(arraySafety) 15 | } 16 | 17 | private fun ArrayWithIterator() { 18 | var arrayAny = arrayOf(1, 2, 3, 4, 5) 19 | withIterator(arrayAny) 20 | } 21 | 22 | private fun classic() { 23 | var i = 5 24 | while (0 <= i) { 25 | println(i--) 26 | } 27 | } 28 | 29 | private fun doWhile(arraySafety: Array) { 30 | val iterator = arraySafety.iterator() 31 | do { 32 | val y = iterator.next() 33 | println(y) 34 | } while (y != null) // y is visible here! 35 | } 36 | 37 | private fun withIterator(arrayAny: Array) { 38 | val iterator = arrayAny.iterator() 39 | while (iterator.hasNext()) { 40 | val next = iterator.next() 41 | println(next) 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/controlFlow/controlFlow.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/main/java/controlFlow/returnsAndJumps/mainReturnJump.kt: -------------------------------------------------------------------------------- 1 | package controlFlow.returnsAndJumps 2 | 3 | /** 4 | * Created by vicboma on 28/10/15. 5 | * 6 | * Kotlin has three structural jump operators 7 | * - return. By default returns from the nearest enclosing function or function expression. 8 | * — break. Terminates the nearest enclosing loop. 9 | * - continue. Proceeds to the next step of the nearest enclosing loop. 10 | * 11 | */ 12 | fun main(args: Array) { 13 | returnBasic() 14 | inlineReturn() 15 | implicitReturn() 16 | breakLoopContinue() 17 | } 18 | 19 | private fun implicitReturn() { 20 | println("Init implicit return") 21 | listOf(1, 2, 3, 4, 5).forEach { 22 | if (it == 2) { 23 | println("Exit implicit return") 24 | return@forEach 25 | } 26 | println(it) 27 | } 28 | } 29 | 30 | private fun inlineReturn() { 31 | 32 | println("Init inline return") 33 | listOf(1, 2, 3, 4, 5).forEach lit@ { 34 | if (it == 5) { 35 | println("Exit inline return") 36 | return@lit 37 | } 38 | println(it) 39 | } 40 | } 41 | 42 | private fun returnBasic(): Unit { 43 | println("Init Basic return") 44 | for (i in 0..5) { 45 | if (i == 5) { 46 | println("Exit Basic return") 47 | return 48 | } 49 | println(i) 50 | } 51 | 52 | } 53 | 54 | private fun breakLoopContinue() { 55 | println("Init breakLoopContinue Loop") 56 | Continue@ for (i in 1..100) { 57 | for (j in 1..100) { 58 | if (j === 50) { 59 | break@Continue 60 | } 61 | } 62 | } 63 | println("Exit breakLoopContinue Loop") 64 | } -------------------------------------------------------------------------------- /src/main/java/dataClasses/dataClasses.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/main/java/dataClasses/src/Customer.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by vicboma on 27/10/15. 3 | */ 4 | public class Customer(val id : Int, val name : String, val email : String) { 5 | } -------------------------------------------------------------------------------- /src/main/java/dataClasses/src/DataCustomer.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by vicboma on 27/10/15. 3 | */ 4 | /** 5 | * The primary constructor needs to have at least one parameter; 6 | * All primary constructor parameters need to be marked as val or var; 7 | * Data classes cannot be abstract, open, sealed or inner; 8 | * Data classes may not extend other classes (but may implement interfaces). 9 | */ 10 | data public class DataCustomer(val id : Int, val name : String, val email : String) { 11 | } -------------------------------------------------------------------------------- /src/main/java/dataClasses/src/mainDataClass.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by vicboma on 27/10/15. 3 | */ 4 | fun main(args: Array) { 5 | templateExamples() 6 | } 7 | 8 | private fun templateExamples() { 9 | val customer1 = Customer(1, "victor", "victorbolinchesmarin@gmail.com") 10 | val customer2 = Customer(1, "victor", "victorbolinchesmarin@gmail.com") 11 | val customer3 = customer1; 12 | val dataCustomer4 = DataCustomer(1, "victor", "victorbolinchesmarin@gmail.com") 13 | val dataCustomer5 = dataCustomer4; 14 | 15 | println(customer1) //toString 16 | println(customer1.hashCode()) //hashCode 17 | println(customer1.equals(customer2)) //equals() 18 | println(customer1.equals(customer3)) 19 | 20 | //The compiler automatically derives the following members from all properties declared in the primary constructor: 21 | //equals()/hashCode() pair, 22 | //toString() of the form "User(name=John, age=42)", 23 | //componentN() functions corresponding to the properties in their order or declaration, 24 | //copy() function (see below). 25 | println(dataCustomer4.equals(dataCustomer5)) 26 | println(dataCustomer4) 27 | } -------------------------------------------------------------------------------- /src/main/java/extensionFunctionBasics/extensionFunctionBasics.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/main/java/extensionFunctionBasics/src/mainExtensionFunction.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by vicboma on 27/10/15. 3 | */ 4 | fun main(args: Array) { 5 | val str = functionClassic() 6 | functionInvokeInstance(str) 7 | } 8 | 9 | private fun functionInvokeInstance(str: String) { 10 | val convertSpacesToUnderscoresInvokeInstanceString = str.convertSpacesToUnderscoresInvokeInstanceString() 11 | println(convertSpacesToUnderscoresInvokeInstanceString) 12 | } 13 | 14 | private fun functionClassic(): String { 15 | val str = "this is my text" 16 | val convertSpacesToUnderscores = convertSpacesToUnderscores(str); 17 | println(convertSpacesToUnderscores) 18 | return str 19 | } 20 | 21 | 22 | fun convertSpacesToUnderscores(str: String) : String { 23 | return str.replace(" ","_") 24 | } 25 | 26 | fun String.convertSpacesToUnderscoresInvokeInstanceString() : String { 27 | return this.replace(" ","_") 28 | } -------------------------------------------------------------------------------- /src/main/java/functionBasics/functionBasics.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/main/java/functionBasics/src/NumberOperations.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by vicboma on 27/10/15. 3 | */ 4 | public class NumberOperations{ 5 | 6 | 7 | // fun addTwoNumbers(first:Int, second :Int) = first + second //Equivalente 8 | 9 | fun addTwoNumbers(first:Int, second :Int) : Int { 10 | val result = first + second 11 | return result 12 | } 13 | 14 | //fun addTwoNumbersDefault(first:Int, second :Int = 0) = first + second //Equivalente 15 | 16 | fun addTwoNumbersDefault(first:Int, second :Int = 0) : Int { 17 | val result = first + second 18 | return result 19 | } 20 | 21 | inline fun reverseList(list : List) : Array { 22 | return list.reversed().toTypedArray(); 23 | } 24 | 25 | fun printSum(a: Int, b: Int): Unit { 26 | println(a + b) 27 | } 28 | 29 | //Funciones de Orden Superior y Expresiones Lambda (como en Haskell :D) 30 | 31 | val sumLambda : (Int, Int) -> Int = { x,y -> x + y} 32 | 33 | fun sumInFunction(x : Int, y : Int, f:(Int, Int) -> Int) : Int { 34 | return f(x,y) 35 | } 36 | 37 | } -------------------------------------------------------------------------------- /src/main/java/functionBasics/src/mainFunction.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by vicboma on 27/10/15. 3 | */ 4 | fun main(args: Array) { 5 | val numberOperations = NumberOperations() 6 | 7 | twoNumbers(numberOperations) 8 | twoNumberReverse(numberOperations) 9 | addTwoNumbersDefault(numberOperations) 10 | reverseList(numberOperations) 11 | reverseList2(numberOperations) 12 | sumLambda(numberOperations) 13 | sumLambdainFunction(numberOperations) 14 | sumInlineOperation(numberOperations) 15 | } 16 | 17 | private fun sumInlineOperation(numberOperations: NumberOperations) { 18 | val sumInFunction = numberOperations.sumInFunction(5, 9, { x, y -> x + y }) 19 | println(sumInFunction) 20 | } 21 | 22 | private fun sumLambdainFunction(numberOperations: NumberOperations) { 23 | val _sumLambda: (Int, Int) -> Int = { x, y -> x + y } 24 | val _sumInFunction = numberOperations.sumInFunction(9, 9, _sumLambda) 25 | println(_sumInFunction) 26 | } 27 | 28 | private fun sumLambda(numberOperations: NumberOperations) { 29 | val sumLambda = numberOperations.sumLambda(2, 2) 30 | println(sumLambda) 31 | } 32 | 33 | private fun reverseList2(numberOperations: NumberOperations) { 34 | val reverseList1 = numberOperations.reverseList(listOf(1, 2, 3, 4)).asList() 35 | println(reverseList1) 36 | } 37 | 38 | private fun reverseList(numberOperations: NumberOperations) { 39 | val reverseList = numberOperations.reverseList(arrayListOf(1, 2, 3)).asList() 40 | println(reverseList) 41 | } 42 | 43 | private fun addTwoNumbersDefault(numberOperations: NumberOperations) { 44 | val addTwoNumbersDefault = numberOperations.addTwoNumbersDefault(2) 45 | println(addTwoNumbersDefault) 46 | } 47 | 48 | private fun twoNumbers(numberOperations: NumberOperations) { 49 | val addTwoNumbers = numberOperations.addTwoNumbers(first = 2, second = 5) 50 | println(addTwoNumbers) 51 | } 52 | 53 | private fun twoNumberReverse(numberOperations: NumberOperations) { 54 | val addTwoNumbers1 = numberOperations.addTwoNumbers(second = 5, first = 2) 55 | println(addTwoNumbers1) 56 | } 57 | 58 | -------------------------------------------------------------------------------- /src/main/java/helloWorld/helloWorld.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/main/java/helloWorld/src/PrintHelloWorld.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by vicboma on 26/10/15. 3 | */ 4 | public class PrintHelloWorld{ 5 | companion object { val HELLO_WORLD = "Hello World " } 6 | public fun basic(name:String){ 7 | println(HELLO_WORLD+name) 8 | } 9 | 10 | public fun basicEmpty(name:String = ""){ 11 | println(HELLO_WORLD+name) 12 | } 13 | 14 | public fun empty(){ 15 | println(HELLO_WORLD) 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/helloWorld/src/mainHelloWorld.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by vicboma on 26/10/15. 3 | */ 4 | fun main(args: Array) { 5 | val printHelloWorld = PrintHelloWorld() 6 | basic(printHelloWorld) 7 | basicEmpty(printHelloWorld) 8 | empty(printHelloWorld) 9 | } 10 | 11 | private fun empty(printHelloWorld: PrintHelloWorld) { 12 | printHelloWorld.empty() 13 | } 14 | 15 | private fun basicEmpty(printHelloWorld: PrintHelloWorld) { 16 | printHelloWorld.basicEmpty() 17 | } 18 | 19 | private fun basic(printHelloWorld: PrintHelloWorld) { 20 | val name = "Victor" 21 | printHelloWorld.basic(name) 22 | } 23 | 24 | 25 | -------------------------------------------------------------------------------- /src/main/java/herencia/herencia.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/main/java/herencia/src/A.kt: -------------------------------------------------------------------------------- 1 | package herencia.src 2 | 3 | /** 4 | * Created by vicboma on 05/11/15. 5 | */ 6 | interface A { 7 | fun buzz() :String 8 | } -------------------------------------------------------------------------------- /src/main/java/herencia/src/B.kt: -------------------------------------------------------------------------------- 1 | package herencia.src 2 | 3 | /** 4 | * Created by vicboma on 05/11/15. 5 | */ 6 | class B(val name:String) : Z("Z"), W { 7 | override val fizBuzz : Int = 29 8 | override fun fiz() : String = "B - fiz" 9 | override fun buzz() : String = "B - buzz" 10 | override fun toString() : String = "ToString() : ${this.name} and my parent is ${super.toString()}" 11 | } -------------------------------------------------------------------------------- /src/main/java/herencia/src/C.kt: -------------------------------------------------------------------------------- 1 | package herencia.src 2 | 3 | /** 4 | * Created by vicboma on 05/11/15. 5 | */ 6 | class C : Z("I am Z"), W { 7 | override val fizBuzz : Int = 92 8 | override fun fiz() : String = "C - fiz" 9 | override fun buzz() : String = "C - buzz" 10 | } -------------------------------------------------------------------------------- /src/main/java/herencia/src/H.kt: -------------------------------------------------------------------------------- 1 | package herencia.src 2 | 3 | /** 4 | * Created by vicboma on 05/11/15. 5 | */ 6 | class H(val name:String):Z("I am Z") { 7 | override fun fiz() :String = super.fiz() 8 | override fun buzz() : String = super.buzz() 9 | } 10 | 11 | -------------------------------------------------------------------------------- /src/main/java/herencia/src/W.kt: -------------------------------------------------------------------------------- 1 | package herencia.src 2 | 3 | /** 4 | * Created by vicboma on 05/11/15. 5 | */ 6 | interface W { 7 | val fizBuzz: Int 8 | } -------------------------------------------------------------------------------- /src/main/java/herencia/src/X.kt: -------------------------------------------------------------------------------- 1 | package herencia.src 2 | 3 | /** 4 | * Created by vicboma on 05/11/15. 5 | */ 6 | 7 | interface X{ 8 | fun fiz() : String 9 | } 10 | 11 | -------------------------------------------------------------------------------- /src/main/java/herencia/src/Z.kt: -------------------------------------------------------------------------------- 1 | package herencia.src 2 | 3 | /** 4 | * Created by vicboma on 05/11/15. 5 | */ 6 | open class Z(val arg:String) : A, X { 7 | override fun toString() = "ToString() : $arg" 8 | override fun fiz() : String = "Z - fiz" 9 | override fun buzz() : String = "Z - buzz" 10 | } 11 | 12 | -------------------------------------------------------------------------------- /src/main/java/herencia/src/herencia.kt: -------------------------------------------------------------------------------- 1 | import herencia.src.B 2 | import herencia.src.C 3 | import herencia.src.H 4 | import herencia.src.Z 5 | 6 | /** 7 | * Created by vicboma on 26/10/15. 8 | */ 9 | fun main(args: Array) { 10 | 11 | val z = Z("I am Z") 12 | val zToString = z.toString() 13 | println(zToString) 14 | val zFizz = z.fiz() 15 | println(zFizz) 16 | val zBuzz = z.buzz() 17 | println(zBuzz) 18 | 19 | 20 | val b = B("I am B") 21 | val bToString = b.toString() 22 | println(bToString) 23 | val bFizz = b.fiz() 24 | println(bFizz) 25 | val bBuzz = b.buzz() 26 | println(bBuzz) 27 | 28 | val bValue = b.fizBuzz 29 | println("b.class.toString() $bValue") 30 | 31 | val c = C() 32 | val cToString = c.toString() 33 | println(cToString) 34 | val cFizz = c.fiz() 35 | println(cFizz) 36 | val cBuzz = c.buzz() 37 | println(cBuzz) 38 | val cValue = c.fizBuzz 39 | println("c.class.toString() $cValue") 40 | 41 | val h = H("I am H") 42 | val hToString = h.toString() 43 | println(hToString) 44 | val hFizz = h.fiz() 45 | println(hFizz) 46 | val hBuzz = h.buzz() 47 | println(hBuzz) 48 | } -------------------------------------------------------------------------------- /src/main/java/iinterface/interface.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/main/java/iinterface/src/A.kt: -------------------------------------------------------------------------------- 1 | package herencia.src 2 | 3 | /** 4 | * Created by vicboma on 05/11/15. 5 | */ 6 | interface AA { 7 | fun buzz() :String 8 | } -------------------------------------------------------------------------------- /src/main/java/iinterface/src/W.kt: -------------------------------------------------------------------------------- 1 | package herencia.src 2 | 3 | /** 4 | * Created by vicboma on 05/11/15. 5 | */ 6 | interface WW { 7 | val fizBuzz: Int 8 | } -------------------------------------------------------------------------------- /src/main/java/iinterface/src/X.kt: -------------------------------------------------------------------------------- 1 | package herencia.src 2 | 3 | /** 4 | * Created by vicboma on 05/11/15. 5 | */ 6 | 7 | interface XX{ 8 | fun fiz() : String 9 | } 10 | 11 | -------------------------------------------------------------------------------- /src/main/java/iinterface/src/Z.kt: -------------------------------------------------------------------------------- 1 | package herencia.src 2 | 3 | /** 4 | * Created by vicboma on 05/11/15. 5 | */ 6 | open class ZZ(val arg:String) : A, X { 7 | override fun toString() = "ToString() : $arg" 8 | override fun fiz() : String = "ZZ - fiz" 9 | override fun buzz() : String = "ZZ - buzz" 10 | } 11 | 12 | -------------------------------------------------------------------------------- /src/main/java/iinterface/src/interfaceMain.kt: -------------------------------------------------------------------------------- 1 | import herencia.src.ZZ 2 | 3 | /** 4 | * Created by vicboma on 26/10/15. 5 | */ 6 | fun main(args: Array) { 7 | val z = ZZ("I am Z") 8 | val zToString = z.toString() 9 | println(zToString) 10 | val zFizz = z.fiz() 11 | println(zFizz) 12 | val zBuzz = z.buzz() 13 | println(zBuzz) 14 | } -------------------------------------------------------------------------------- /src/main/java/infixFunctionBasics/infixFunctionBasics.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/main/java/infixFunctionBasics/src/mainInfixFunctionBasics.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by vicboma on 26/10/15. 3 | */ 4 | fun main(args: Array) { 5 | val list = listOf(10, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5) 6 | 7 | doubleResult(list) 8 | filter(list) 9 | map(list) 10 | } 11 | 12 | //infix without paren + brace 13 | private fun map(list: List) = list.filter { it % 2 == 0 }.map { it - 1 }.forEach { println(it) } 14 | 15 | //infix without Dots & paren + brace 16 | private fun filter(list: List) = list.filter { it % 2 == 0 }.forEach { println(it) } 17 | 18 | //infix with paren & brace 19 | private fun doubleResult(list: List) = list.forEach({ println(it * 2) }) 20 | 21 | 22 | -------------------------------------------------------------------------------- /src/main/java/nullSafetyOperators/operators.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/main/java/nullSafetyOperators/src/mainNullsafetyOperators.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by vicboma on 27/10/15. 3 | */ 4 | fun main(args: Array) { 5 | checkingForNullcondition() 6 | val listMutable = safetyAccess() 7 | elvisOperator(listMutable) 8 | safetyCasts() 9 | specialOperator() 10 | } 11 | 12 | private fun specialOperator() { 13 | val str: String? = null 14 | val len = str!!.length //accedemos a la propiedad, pero dará una excepción. 15 | } 16 | 17 | private fun safetyCasts() { 18 | val a: Double = 2.0 19 | val aInt: Int? = a as? Int 20 | println(aInt) 21 | } 22 | 23 | private fun elvisOperator(listMutable: Array) { 24 | val result = listMutable?.size ?: -1 25 | println(result) 26 | } 27 | 28 | //Safe Calls - Any"?".property 29 | private fun safetyAccess(): Array { 30 | val listMutable = arrayOf(1, 2, 3, 4) 31 | 32 | val _size = listMutable?.size 33 | if (listMutable != null && _size > 0) 34 | println("Array of size $_size") 35 | else 36 | println("Empty Array") 37 | return listMutable 38 | } 39 | 40 | private fun checkingForNullcondition() { 41 | val listImmutable = listOf(1, 2, 3, 4) 42 | 43 | val size = listImmutable.size 44 | if (listImmutable != null && size > 0) 45 | println("Array of size $size") 46 | else 47 | println("Empty Array") 48 | } 49 | 50 | -------------------------------------------------------------------------------- /src/main/java/staticValue/src/State.kt: -------------------------------------------------------------------------------- 1 | package staticValue.src 2 | 3 | /** 4 | * Created by vicboma on 28/10/15. 5 | */ 6 | public class State { 7 | companion object { 8 | val START = 1 9 | val STOP = 0 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/staticValue/src/mainStaticValue.kt: -------------------------------------------------------------------------------- 1 | import staticValue.src.State 2 | 3 | /** 4 | * Created by vicboma on 27/10/15. 5 | */ 6 | fun main(args: Array) { 7 | start() 8 | stop() 9 | } 10 | 11 | fun start() = println("State static: ${State.START}") 12 | fun stop() = println("State static: ${State.STOP}") 13 | 14 | -------------------------------------------------------------------------------- /src/main/java/staticValue/staticValue.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/main/java/strings/src/mainString.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by vicboma on 26/10/15. 3 | */ 4 | fun main(args: Array) { 5 | literals() 6 | templates() 7 | } 8 | 9 | private fun templates() { 10 | val number = 18 11 | val name = "vicboma $number" 12 | println(name) 13 | 14 | println("$name.length = ${name.length}") 15 | 16 | val price = "${'$'}9.99 = 9.99 dollars" 17 | println(price) 18 | } 19 | 20 | private fun literals() { 21 | val helloWorld = "Hello, world!" 22 | println(helloWorld) 23 | val str = "Hello, world!\n..." // w/backslash 24 | println(str) 25 | } -------------------------------------------------------------------------------- /src/main/java/strings/strings.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/main/java/variableBasics/src/mainVariable.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by vicboma on 26/10/15. 3 | */ 4 | fun main(args: Array) { 5 | immutableValue() 6 | mutableValue() 7 | nullableValue() 8 | } 9 | 10 | private fun immutableValue() { 11 | val name = "Victor" 12 | //immutable prototype 13 | //val name : String = "Victor" 14 | // name = "Victor Manuel" Error! 15 | println(name) 16 | 17 | } 18 | 19 | private fun mutableValue() { 20 | //mutable prototype 21 | var lastName: String 22 | lastName = "Victor Bolinches" 23 | println(lastName) 24 | 25 | } 26 | 27 | private fun nullableValue() { 28 | //var lastNameNullable: String Error! 29 | //lastNameNullable = null Error! 30 | 31 | var lastNameNullable: String? 32 | lastNameNullable = null 33 | println(lastNameNullable) 34 | lastNameNullable = "Victor Manuel Bolinches" 35 | println(lastNameNullable) 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/variableBasics/variableBasics.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/main/test/booleanOperators/src/BooleanOperatorTest.kt: -------------------------------------------------------------------------------- 1 | package classBasics.src 2 | 3 | import booleanOperators.src.BooleanOperator 4 | import org.junit.After 5 | import org.junit.Assert.assertTrue 6 | import org.junit.Before 7 | import org.junit.Test 8 | 9 | 10 | /** 11 | * Created by vicboma on 27/10/15. 12 | */ 13 | class BooleanOperatorTest { 14 | 15 | companion object { 16 | val EXPECTED_DISJUNCTION = arrayOf(false,true,true,true) 17 | val EXPECTED_CONJUNCTION = arrayOf(false,false,false,true) 18 | val EXPECTED_NEGATION = arrayOf(true,false) 19 | } 20 | 21 | @Before 22 | fun setUp() { 23 | } 24 | 25 | @After 26 | fun tearDown() { 27 | } 28 | 29 | @Test 30 | fun testDisjunction() { 31 | val list = arrayOf( 32 | BooleanOperator.lazyDisjunction(false, false), 33 | BooleanOperator.lazyDisjunction(false, true), 34 | BooleanOperator.lazyDisjunction(true, false), 35 | BooleanOperator.lazyDisjunction(true, true) 36 | ) 37 | 38 | var i = 0 39 | for(result in list){ 40 | assertTrue("Fail disjunction",result === EXPECTED_DISJUNCTION.get(i)) 41 | i++ 42 | } 43 | } 44 | 45 | @Test 46 | fun testConjunction() { 47 | val list = arrayOf( 48 | BooleanOperator.lazyConjunction(false, false), 49 | BooleanOperator.lazyConjunction(false, true), 50 | BooleanOperator.lazyConjunction(true, false), 51 | BooleanOperator.lazyConjunction(true, true) 52 | ) 53 | 54 | var i = 0 55 | for(result in list){ 56 | assertTrue("Fail conjunction", result === EXPECTED_CONJUNCTION.get(i)) 57 | i++ 58 | } 59 | } 60 | 61 | @Test 62 | fun testNegation() { 63 | val list = arrayOf( 64 | BooleanOperator.negation(false), 65 | BooleanOperator.negation(true) 66 | ) 67 | 68 | var i = 0 69 | for(result in list){ 70 | assertTrue("Fail negation", result === EXPECTED_NEGATION.get(i)) 71 | i++ 72 | } 73 | } 74 | } -------------------------------------------------------------------------------- /src/main/test/classBasics/src/CustomerClassTest.kt: -------------------------------------------------------------------------------- 1 | package classBasics.src 2 | 3 | import CustomerClass 4 | import org.junit.After 5 | import org.junit.Assert.assertFalse 6 | import org.junit.Before 7 | import org.junit.Test 8 | import org.junit.Assert.assertTrue 9 | 10 | 11 | /** 12 | * Created by vicboma on 27/10/15. 13 | */ 14 | class CustomerClassTest { 15 | 16 | companion object { val EXPECTED_ID = 1 } 17 | 18 | @Before 19 | fun setUp() { 20 | } 21 | 22 | @After 23 | fun tearDown() { 24 | } 25 | 26 | @Test 27 | fun testGetId() { 28 | val customer = CustomerClass(EXPECTED_ID) 29 | assertTrue("Fail id",EXPECTED_ID == customer.id) 30 | } 31 | 32 | @Test 33 | fun testGetName() { 34 | val customer = CustomerClass(EXPECTED_ID) 35 | assertTrue("Fail name","" === customer.name) 36 | } 37 | 38 | @Test 39 | fun testSetName() { 40 | val customer = CustomerClass(EXPECTED_ID) 41 | val test = "test" 42 | customer.name = test 43 | assertTrue("Fail set name", test.equals(customer.name) ) 44 | assertTrue("Fail set name", test == customer.name ) 45 | assertTrue("Fail set name", test === customer.name ) 46 | } 47 | 48 | @Test 49 | fun testDoSomething() { 50 | val expected = "Some code" 51 | val customer : CustomerClass = CustomerClass(EXPECTED_ID) 52 | val doSomething = customer.doSomething() 53 | assertTrue("Fail doSomething", doSomething === expected ) 54 | } 55 | 56 | @Test 57 | fun testToString() { 58 | val customer : CustomerClass = CustomerClass(EXPECTED_ID) 59 | val toString = customer.toString() 60 | assertFalse("Fail toString", toString === EXPECTED_ID.toString()) 61 | assertTrue("Fail toString", toString == EXPECTED_ID.toString() ) 62 | assertTrue("Fail toString", toString.equals(EXPECTED_ID.toString())) 63 | } 64 | } -------------------------------------------------------------------------------- /src/main/test/classBasics/src/EmployeeTest.kt: -------------------------------------------------------------------------------- 1 | package classBasics.src 2 | 3 | import Employee 4 | import junit.framework.Assert.assertTrue 5 | import org.junit.After 6 | import org.junit.Before 7 | import org.junit.Test 8 | 9 | /** 10 | * Created by vicboma on 28/10/15. 11 | */ 12 | class EmployeeTest { 13 | 14 | companion object { 15 | val EXPECTED_ID = 1 16 | val EXPECTED_NAME = "Victor" 17 | } 18 | 19 | @Before 20 | fun setUp() { 21 | } 22 | 23 | @After 24 | fun tearDown() { 25 | } 26 | 27 | @Test 28 | fun testGetId() { 29 | val employee = Employee(EXPECTED_ID,EXPECTED_NAME) 30 | val id = employee.id 31 | assertTrue("Fail id", id === EXPECTED_ID ) 32 | } 33 | 34 | @Test 35 | fun testGetName() { 36 | val employee = Employee(name = EXPECTED_NAME, id = EXPECTED_ID) 37 | val name = employee.name 38 | assertTrue("Fail name", name === EXPECTED_NAME) 39 | assertTrue("Fail name", name == EXPECTED_NAME) 40 | assertTrue("Fail name", name.equals(EXPECTED_NAME)) 41 | } 42 | } -------------------------------------------------------------------------------- /src/main/test/staticValue/src/StateTest.kt: -------------------------------------------------------------------------------- 1 | package staticValue.src 2 | 3 | import junit.framework.Assert.assertTrue 4 | import org.junit.Test 5 | 6 | /** 7 | * Created by vicboma on 30/10/15. 8 | * 9 | * Test with closure fun 10 | */ 11 | class StateTest{ 12 | 13 | @Test 14 | fun testStop(){ 15 | val expected = 0; 16 | assertTrue( 17 | "Fail Stop", 18 | State.STOP == expected 19 | ) 20 | } 21 | 22 | @Test 23 | fun testStart(){ 24 | val expected = 1; 25 | assertTrue( 26 | "Fail Start", 27 | State.START == expected 28 | ) 29 | } 30 | } --------------------------------------------------------------------------------