---
# System prepended metadata

title: Kotlin demo

---

# Kotlin demo

### print() and println()
```kotlin=
fun main() {
    println("Hello World!")
}
```

```kotlin=
fun main() {
    print("Hello")
    println(" World!")
}
```

### var and val
```kotlin=
fun main() {
    val a = 3
    a = 9 // Will have error
}
```


### Overflow
```kotlin=
fun main() {
    var i : Int = 2147483648 // 2^32
}
```


### Conditional

#### if
```kotlin=
fun main() {
  val a = 3
  
  if (a > 1 && a < 7) {
      println("In the range!")
  } 
  else if (a == 3) { // Won't execute if first "if" reach
      println("a equals to 3")
  }
  
  if (a == 3) {
      println("In second if block")
  }
}
```
#### when
```kotlin=
fun main() {
    var a = 9
    
    when (a) {
        9 -> println("a is 9.")
        else -> println("I don't know the number")
    }
}
```

```kotlin=
fun main() {
    var a = 5
    
    when (a) { 
        9 -> { // We can write code in the block
            println("a is 9.")
        }
        else -> {
            println("I don't know the number.")
        }
    }
}
```
### Loop

#### for
```kotlin=
fun main() {
    for (i in 1..6) {
        print("${i} ") // Add space
    }
    println() // Break line
     
    for (i in 1..6 step 2) {
    	print("${i} ") // Add space
    }
    println() // Break line
}
```

#### while
```kotlin=
fun main() {
    var count = 1
    
    while (count <= 5) {
        print("${count} ")
        count++
    }
    
    println() // break line
}
```

#### Continue
```kotlin=
fun main() {
    for(i in 1..5) {
	    if(i == 3) continue // Jump through 3
        
        print("${i} ")
    }
    println() // break line
}
```
### Container
#### Array

```kotlin=
fun main() {
    var a_int_array : IntArray = intArrayOf(1, 2, 3)
	
    println(a_int_array[0]) // First element
    println(a_int_array[3]) // Out of range
}
```
#### Read-Only (List for example)

```kotlin=
fun main() {
    val list_1: List<Int> = listOf(1, 2, 3)

    println(list_1[0])
}
```
#### Mutable (MutableMap for example)
```kotlin=
fun main() {
    var mutablemap: MutableMap<String, String> = mutableMapOf()
    
    mutablemap.put("a", "boy") //add data
    mutablemap.put("b", "girl") //add data
    
    println(mutablemap) // Print whole map
    println(mutablemap["a"]) // Print the value of key "a"
    
    mutablemap.remove("a") //remove data
    mutablemap.clear() //delete all data
    
    println(mutablemap) // Print whole map
}
```
### Function
```kotlin=
fun multi(a: Int, b: Int = 1): Int{
    return a * b
}

fun main() {
    var result_1 = multi(2, 3)
    var result_2 = multi(2)
    
    println(result_1)
    println(result_2)
}
```
#### Inline Function
```kotlin=
fun multi(a: Int, b: Int = 1): Int = a * b

fun main() {
    var result_3 = multi(b = 4, a = 5)
    println(result_3)
}
```

### Class

```kotlin=
class Data {
    var id : String = ""
    var gender : String = "boy" // Default gender is boy.
}

fun main() {
    val customer = Data()
    customer.id = "no.12" // Default “.” to access the member
    customer.gender = "girl"
    
    println(customer.id)
    println(customer.gender)
}
```

#### Init
```kotlin=
class Test(test: Int) {
    // Should init the type of the instances
    var test_num: Int

    // Run immediately after building class
    init {
        println("Here is the init block.")
        test_num = test
    }
}

fun main() {
    var test_class = Test(3)
    println(test_class.test_num)
}
```

```kotlin=
class Test (var test_num: Int) {
    // Run immediately after building class
    init { println("Here is the init block.") }
}

fun main() {
    // Init the object
    var test_2 = Test(2)
    println(test_2.test_num)
}
```

#### Data class
```kotlin=
// Data class
data class User(val name: String,val i:Int, val id: Int)            

fun main() {
    val user = User("Alex",10, 1) 
    println(user)                                          
    println("name = ${user.component1()}")   // Print the first component in User       
    println("id = ${user.component3()}") // Print the second component in User
}
```