Kotlin lernen – Cheat Sheet Teil 1

Ich selbst habe bisher hauptsächlich in Java und Python programmiert doch insbesondere die Android Welt wendet sich immer mehr Kotlin zu. Daher habe ich mich entschlossen diesen Artikel zu schreiben und so kompakt wie möglich die Kotlin Syntax vorzustellen.

Kotlin: Hello World


package com.robinhenniges.kotlin

fun main(args : Array){
println("Hello World!")
}

Kotlin Variables

 


package com.robinhenniges.kotlin


fun main(args : Array<string>){

var intVar : Int =  1234
var longVar :Long = 123456789
var floatVar : Float = 21.22f
var doubleVar : Double = 2.2

var aLetter : Char = 'A'
var myBool : Boolean = true
var myLetter : String  = "itIsSuberescred"
myLetter+= " attack"

val myName = "Rob"

println("Numbers: $intVar $longVar $floatVar $doubleVar")
println("Char/Boolean/String: $aLetter $myBool $myLetter $myName")
}

Kotlin String und Ausgabe


package com.robinhenniges.kotlin

fun main(args : Array<string>){

val myName = "Rob"

println("Name: $myName") 
println("Der erste Buchstabe ist: ${myName[0]}") 

}

Kotlin Arrays / Lists

Kotlin Methode und Variable


package com.robinhenniges.kotlin

fun main(args : Array<string>){

val myArray = arrayOf(1,2,3, "rob")

var firstElem1 = myArray[0]
var firstElem2 = myArray.first()

println("Die first Methode is praktisch: ${firstElem1.equals(firstElem2)}")

var sizeOfArray = myArray.size

}

Kotlin Ranges


package com.robinhenniges.kotlin

fun main(args : Array<string>){

   val oneToTen = 1..10
   val aToZ = "A".."Z"

   println("Is R in aToZ ? ${"R" in aToZ}")

   val fiveToOne = 5.downTo(1)
   val mySteps = oneToTen.step(2)

   for (number in mySteps) print("$number ")
    
   println("")
   
   println("$fiveToOne outputs: ${fiveToOne.joinToString(", ")}")
    
}

Result:

Is R in aToZ ? true
1 3 5 7 9 
5 downTo 1 step 1 outputs: 5, 4, 3, 2, 1

Kotlin if else (Control Flow)


package com.robinhenniges.kotlin

fun main(args : Array<string>){

  val person1Credit = 2000f // float
  val person1City = "Munich"
   
  val person2Credit = 1000f // float
  val person2City = "Berlin"
   
  val person3Credit = 2000f // float
  val person3City = "Köln"
    
  credit(person1Credit, person1City)
  credit(person2Credit, person2City)
  credit(person3Credit, person3City)

}

fun credit(credit: Float, city:String) {
     val bonusCity = arrayOf("Berlin", "Hamburg", "Munich")  

     if (credit > 1000 && city in bonusCity) {
      println( "You have to pay 0.03% in total: ${credit * 1.03}" )
  } else {
      println( "You have to pay 0.01% in total: ${credit * 1.01}" )
  } 
}

Result:

You have to pay 0.03% in total: 2060.0
You have to pay 0.01% in total: 1010.0
You have to pay 0.01% in total: 2020.0

Kotlin when, for (Control Flow)

Switch case in Kotlin is replaced by when.


package com.robinhenniges.kotlin

fun main(args : Array<string>){

  val codes = arrayOf("12349", "12347", "12345", "00000")
    
  for ((index, postalCode) in codes.withIndex()){
      print ("Processing element $index: ")
    when (postalCode) {
      "12345" -> println ("This postalcode does not exist")
      "12349","12347" -> println("This a valid Berlin postal code")
      "12347" -> println("This never gets called")
      else -> println("We have no information about this postal code")
  	}    
  }
}

Result:

Processing element 0: This a valid Berlin postal code
Processing element 1: This a valid Berlin postal code
Processing element 2: This postalcode does not exist
Processing element 3: We have no information about this postal code

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.