본문 바로가기

Study/Kotlin

Kotlin 기본 문법 따라하기 - 1. Basic Syntax

Kotlin 기본 문법 따라하기

 

Basic Syntax - Kotlin Programming Language

 

kotlinlang.org

 

package com.kotlintest.test


import java.lang.Integer.parseInt


class BasicSyntax{


    fun main() {
        println("Hello Kotlin")


        println("sum(1, 2) : ${sum(1, 2)}")
        println("sumOneLine(3, 4) : ${sumOneLine(3, 4)}")
        printSum(5, 6)


        variable()


        println("maxOf(7, 8) : ${maxOf(7, 8)}")
        println("maxOfOneLine(9, 10) : ${maxOfOneLine(9, 10)}")


        printProduct("4", "9")


        forLoop()
        whileLoop()
        whenExpression(1)
        ranges()
        collections()
    }


    fun sum(a: Int, b: Int): Int {
        return a + b
    }


    fun sumOneLine(a: Int, b: Int) = a + b


    fun printSum(a: Int, b: Int) {
        println("sum of $a and $b is ${a+b}")
    }




    fun variable() {


        val a: Int = 1  // immediate assignment
        val b = 2   // `Int` type is inferred
        val c: Int  // Type required when no initializer is provided
        c = 3       // deferred assignment




        val PI = 3.14
        var x = 5
        x += 1
    }




    fun maxOf (a: Int, b: Int) : Int {
        if (a > b) {
            return a
        } else {
            return b
        }
    }


    fun maxOfOneLine (a: Int, b: Int) = if (a > b) a else b




    fun printProduct(arg1: String, arg2: String) {
        val x = parseInt(arg1)
        val y = parseInt(arg2)


        // Using `x * y` yields error because they may hold nulls.
        if (x != null && y != null) {
            // x and y are automatically cast to non-nullable after null check
            println(x * y)
        }
        else {
            println("'$arg1' or '$arg2' is not a number")
        }
    }




    fun forLoop() {


        println("---------- start for loop ----------")


        val items = listOf("apple", "banana", "kiwifruit")
        for (item in items) {
            println(item)
        }


        for (index in items.indices) {
            println("item at $index is ${items[index]}")
        }


        println("---------- end for loop ----------")
    }




    fun whileLoop() {


        println("---------- start while loop ----------")


        val items = listOf("apple", "banana", "kiwifruit")
        var index = 0
        while (index < items.size) {
            println("item at $index is ${items[index]}")
            index++
        }


        println("---------- end while loop ----------")
    }




    fun whenExpression(obj: Any) {


        println("---------- start when expression ----------")


        when (obj) {
            1           -> println("One")
            "Hello"     -> println("Greeting")
            is Long     -> println("Long")
            !is String  -> println("Not a string")
            else        -> println("Unknown")
        }


        println("---------- end when expression ----------")
    }




    fun ranges() {
        println("---------- start ranges ----------")


        val x = 10
        val y = 9
        if (x in 1..y+1) {
            println("fits in range")
        }


        for (x1 in 1..10 step 2) {
            print("$x1 ")
        }


        println()


        for (x2 in 9 downTo 0 step 3) {
            println("$x2 ")
        }


        println("---------- end ranges ----------")
    }




    fun collections() {


        println("---------- start collections ----------")


        val fruits = listOf("banana", "avocado", "apple", "kiwifruit")
        fruits
                .filter { it.startsWith("a") }
                .sortedBy { it }
                .map { it.toUpperCase() }
                .forEach { println(it) }


        println("---------- end collections ----------")


    }
}

 

Hello Kotlin
sum(1, 2) : 3
sumOneLine(3, 4) : 7
sum of 5 and 6 is 11
maxOf(7, 8) : 8
maxOfOneLine(9, 10) : 10
36
---------- start for loop ----------
apple
banana
kiwifruit
item at 0 is apple
item at 1 is banana
item at 2 is kiwifruit
---------- end for loop ----------
---------- start while loop ----------
item at 0 is apple
item at 1 is banana
item at 2 is kiwifruit
---------- end while loop ----------
---------- start when expression ----------
One
---------- end when expression ----------
---------- start ranges ----------
fits in range
1 3 5 7 9 
9 
6 
3 
0 
---------- end ranges ----------
---------- start collections ----------
APPLE
AVOCADO
---------- end collections ----------