{"id":30,"date":"2025-09-25T12:07:26","date_gmt":"2025-09-25T03:07:26","guid":{"rendered":"https:\/\/devserver.kr\/blog\/?p=30"},"modified":"2025-09-25T12:07:26","modified_gmt":"2025-09-25T03:07:26","slug":"kotlin-%eb%ac%b8%eb%b2%95-%ec%a0%95%eb%a6%ac","status":"publish","type":"post","link":"https:\/\/devserver.kr\/blog\/kotlin\/kotlin-%eb%ac%b8%eb%b2%95-%ec%a0%95%eb%a6%ac\/","title":{"rendered":"Kotlin \ubb38\ubc95 \uc815\ub9ac"},"content":{"rendered":"\n<div class=\"wp-block-jetpack-markdown\"><h2>\uae30\ubcf8 \ud0c0\uc785<\/h2>\n<h3>\uc22b\uc790<\/h3>\n<ul>\n<li>Byte : 8-bits<\/li>\n<li>Short : 16-bits<\/li>\n<li>Int : 32-bits<\/li>\n<li>Long : 64-bits<\/li>\n<li>Float : 32-bits<\/li>\n<li>Double : 64-bits<\/li>\n<\/ul>\n<h3>\ubb38\uc790<\/h3>\n<ul>\n<li>Char<\/li>\n<\/ul>\n<h3>\ubd88\ub9b0<\/h3>\n<ul>\n<li>Boolean : true, false<\/li>\n<\/ul>\n<h3>\ubc30\uc5f4<\/h3>\n<ul>\n<li>Array : Array class<\/li>\n<li>ByteArray<\/li>\n<li>ShortArray<\/li>\n<li>IntArray<\/li>\n<\/ul>\n<h3>\ubb38\uc790\uc5f4<\/h3>\n<ul>\n<li>String<\/li>\n<\/ul>\n<pre><code class=\"language-swift\">func func() {\n  var a = 'AAA';\n  return a;\n}\n<\/code><\/pre>\n<h2>\uc0c1\uc218\uc640 \ubcc0\uc218<\/h2>\n<ul>\n<li>val : \uc0c1\uc218<\/li>\n<li>var : \ubcc0\uc218<\/li>\n<\/ul>\n<pre><code class=\"language-kotlin\">val a = 1\nvar b = 2\nb = 3\n<\/code><\/pre>\n<h3>\uc120\uc5b8\ubc95<\/h3>\n<pre><code class=\"language-kotlin\">var a: Int = 5\nlateinit var b:Int\n<\/code><\/pre>\n<h3>nullable \ubcc0\uc218<\/h3>\n<pre><code class=\"language-kotlin\">var a: Int?\n<\/code><\/pre>\n<p>\uc815\uc218\uac12\uc744 \uac00\uc9c0\ub358\uc9c0, null \uc77c \uc218 \uc788\ub2e4.<\/p>\n<hr>\n<h2>Package<\/h2>\n<pre><code class=\"language-kotlin\">package org.example\n<\/code><\/pre>\n<p>\uae30\ubcf8\uc801\uc73c\ub85c import \ub418\ub294 \ud328\ud0a4\uc9c0\ub4e4<\/p>\n<ul>\n<li>kotlin.*<\/li>\n<li>kotlin.annotation.*<\/li>\n<li>kotlin.collections.*<\/li>\n<li>kotlin.comparisons.* (since 1.1)<\/li>\n<li>kotlin.io.*<\/li>\n<li>kotlin.ranges.*<\/li>\n<li>kotlin.sequences.*<\/li>\n<li>kotlin.text.*<\/li>\n<\/ul>\n<h3>import<\/h3>\n<pre><code class=\"language-kotlin\">import org.example.Message\nimport org.example.*\nimport org.test.Message as testMessage \/\/ testMessage stands for 'org.test.Message'\n<\/code><\/pre>\n<hr>\n<h2>\ud750\ub984\uc81c\uc5b4<\/h2>\n<h3>if<\/h3>\n<pre><code class=\"language-kotlin\">\/\/ Traditional usage \nvar max = a \nif (a &lt; b) max = b\n\n\/\/ With else \nvar max: Int\nif (a &gt; b) {\n    max = a\n} else {\n    max = b\n}\n \n\/\/ As expression \nval max = if (a &gt; b) a else b\n<\/code><\/pre>\n<pre><code class=\"language-kotlin\">val max = if (a &gt; b) {\n    print(&quot;Choose a&quot;)\n    a\n} else {\n    print(&quot;Choose b&quot;)\n    b\n}\n<\/code><\/pre>\n<h3>when<\/h3>\n<pre><code class=\"language-kotlin\">when (x) {\n    1 -&gt; print(&quot;x == 1&quot;)\n    2 -&gt; print(&quot;x == 2&quot;)\n    else -&gt; { \/\/ Note the block\n        print(&quot;x is neither 1 nor 2&quot;)\n    }\n}\n<\/code><\/pre>\n<p>range \ub85c \uccb4\ud06c\uac00\ub2a5<\/p>\n<pre><code class=\"language-kotlin\">when (x) {\n    in 1..10 -&gt; print(&quot;x is in the range&quot;)\n    in validNumbers -&gt; print(&quot;x is valid&quot;)\n    !in 10..20 -&gt; print(&quot;x is outside the range&quot;)\n    else -&gt; print(&quot;none of the above&quot;)\n}\n<\/code><\/pre>\n<h3>For Loops<\/h3>\n<pre><code class=\"language-kotlin\">for (item in collection) print(item)\n<\/code><\/pre>\n<pre><code class=\"language-kotlin\">for (item: Int in ints) {\n    \/\/ ...\n}\n<\/code><\/pre>\n<pre><code class=\"language-kotlin\">for (i in 1..3) {\n    println(i)\n}\nfor (i in 6 downTo 0 step 2) {\n    println(i)\n}\n<\/code><\/pre>\n<pre><code class=\"language-kotlin\">for (i in array.indices) {\n    println(array[i])\n}\n<\/code><\/pre>\n<pre><code class=\"language-kotlin\">for ((index, value) in array.withIndex()) {\n    println(&quot;the element at $index is $value&quot;)\n}\n<\/code><\/pre>\n<h3>While Loops<\/h3>\n<pre><code class=\"language-kotlin\">while (x &gt; 0) {\n    x--\n}\n\ndo {\n    val y = retrieveData()\n} while (y != null) \/\/ y is visible here!\n<\/code><\/pre>\n<h2>Functions<\/h2>\n<pre><code class=\"language-kotlin\">fun double(x: Int): Int {\n    return 2 * x\n}\n<\/code><\/pre>\n<pre><code class=\"language-kotlin\">fun read(\n    b: Array&lt;Byte&gt;, \n    off: Int = 0, \n    len: Int = b.size,\n) { \/*...*\/ }\n<\/code><\/pre>\n<pre><code class=\"language-kotlin\">fun foo(\n    bar: Int = 0, \n    baz: Int,\n) { \/*...*\/ }\n\nfoo(baz = 1) \/\/ The default value bar = 0 is used\n<\/code><\/pre>\n<h2>Collections<\/h2>\n<h3>List<\/h3>\n<pre><code class=\"language-kotlin\">val list: List&lt;Int&gt; = List(5, {i -&gt; i})\n\/\/\nval list: List&lt;Int&gt; = listOf(1,2,3,4,5)\n<\/code><\/pre>\n<p>immutable \ud558\uba70, [] \ub85c \uc811\uadfc\uac00\ub2a5<\/p>\n<h3>MutableList<\/h3>\n<pre><code class=\"language-kotlin\">val array1 = MutableList&lt;Int&gt;(5, {i -&gt; i})\narray[0] = 10\n\nval array2 = mutableListOf(1,2,3)\narray2.add(4)\n<\/code><\/pre>\n<h3>Set<\/h3>\n<p>\uc911\ubcf5\uc744 \ud5c8\uc6a9\ud558\uc9c0 \uc54a\uc74c<\/p>\n<pre><code class=\"language-kotlin\">val set1: Set&lt;Int&gt; = setOf&lt;Int&gt;(3,3,1,2)\n<\/code><\/pre>\n<p>3\uc774 \uc911\ubcf5\ub418\uac8c \uc120\uc5b8\uc774 \ub418\uc5b4\ub3c4 set1\uc5d0\ub294 3\uc774 \ud558\ub098\ub9cc \ub4e4\uc5b4\uac04\ub2e4.<\/p>\n<h3>Map<\/h3>\n<p>\ud0a4-\ubc38\ub958 \ud615\ud0dc<\/p>\n<pre><code class=\"language-kotlin\">val map1: Map&lt;String, Int&gt; = mapOf(&quot;One&quot; to 1, &quot;Two&quot; to 2, &quot;Three&quot; to 3)\nval map2: Map&lt;String, Int&gt; = mapOf(Pair(&quot;Four&quot;, 4), Pair(&quot;Five&quot;, 5))\n<\/code><\/pre>\n<h1>Coroutines<\/h1>\n<pre><code class=\"language-kotlin\">import kotlinx.coroutines.*\n\nfun main() {\n    GlobalScope.launch { \/\/ launch a new coroutine in background and continue\n        delay(1000L) \/\/ non-blocking delay for 1 second (default time unit is ms)\n        println(&quot;World!&quot;) \/\/ print after delay\n    }\n    println(&quot;Hello,&quot;) \/\/ main thread continues while coroutine is delayed\n    Thread.sleep(2000L) \/\/ block main thread for 2 seconds to keep JVM alive\n}\n<\/code><\/pre>\n<p><code>GlobalScope.launch{ ... }<\/code> \ub294 <code>thread { .. }<\/code> \ub610\ub294 <code>delay(...)<\/code> with <code>Thread.sleep(...)<\/code> \uc73c\ub85c \ub300\uccb4\uac00\ub2a5<\/p>\n<p><code>delay<\/code>\ub294 suspending function\uc73c\ub85c \ucf54\ub8e8\ud2f4\uc744 \ube14\ub77d\ud55c\ub2e4. \uc989, \ucf54\ub8e8\ud2f4 \uc548\uc5d0\uc11c \uc0ac\uc6a9\ub418\uc5b4\uc57c\ud55c\ub2e4. \uc4f0\ub808\ub4dc \uc548\uc5d0\uc11c\ub294 \uc5d0\ub7ec \ubc1c\uc0dd.<\/p>\n<pre><code class=\"language-kotlin\">import kotlinx.coroutines.*\n\nfun main() { \n    GlobalScope.launch { \/\/ launch a new coroutine in background and continue\n        delay(1000L)\n        println(&quot;World!&quot;)\n    }\n    println(&quot;Hello,&quot;) \/\/ main thread continues here immediately\n    runBlocking {     \/\/ but this expression blocks the main thread\n        delay(2000L)  \/\/ ... while we delay for 2 seconds to keep JVM alive\n    } \n}\n<\/code><\/pre>\n<h1>Date()<\/h1>\n<p>val cal = Calendar.getInstance()\ncal.time = Date()\nval df: DateFormat = SimpleDateFormat(\u201cyyyy-MM-dd\u201d)<\/p>\n<p>cal.add(Calendar.Month, 1)<\/p>\n<h2>\ubc94\uc704 \uc9c0\uc815\ud568\uc218<\/h2>\n<p>apply, run, with, let, also<\/p>\n<p>data class Person (\nvar name: String = \u201c\u201d,\nvar age: Int = 0,\nvar temperature: Float = 36.5f\n)<\/p>\n<h3>apply<\/h3>\n<p>\uc218\uc2e0\uac1d\uccb4 \ub0b4\ubd80 \ud504\ub85c\ud37c\ud2f0\ub97c \ubcc0\uacbd\ud55c \ub2e4\uc74c \uc218\uc2e0\uac1d\uccb4 \uc790\uccb4\ub97c \ubc18\ud658\ud558\uace0\uc790 \ud560 \ub54c<\/p>\n<p>var p = Person().apply {\nname = \u201cnew name\u201d\nage = 29\ntemperature = 36.4f\n}<\/p>\n<h3>run<\/h3>\n<p>apply \uc640 \uc720\uc0ac, \uc218\uc2e0\uac1d\uccb4\ub97c \ub9ac\ud134\ud558\uc9c0\uc54a\uace0, run \ube14\ub85d\uc758 \ub9c8\uc9c0\ub9c9 \ub77c\uc778\uc744 return \ud55c\ub2e4.<\/p>\n<p>data class Person (\nvar name: String = \u201c\u201d,\nvar age: Int = 0,\nvar temperature: Float = 36.5f\n) {\nfun isSick(): Boolean = temperature &gt; 37.5f\n}<\/p>\n<p>val person = Person(name = \u201cFB\u201d, age = 29, temperature = 36.5f)\nval isPersonSick = person.run {\ntemperature = 37.2f\nisSick() \/\/ \uc774 \ubd80\ubd84\uc774 return\n}<\/p>\n<h3>with<\/h3>\n<p>run \uacfc \ub3d9\uc77c, run\uc740 \ud655\uc7a5\ud568\uc218\ub85c \uc0ac\uc6a9\ub418\uc9c0\ub9cc with\ub294 \uc218\uc2e0\uac1d\uccb4\ub97c \uc778\uc790\ub85c \ubc1b\uc544 \uc0ac\uc6a9\ud558\ub294 \ucc28\uc774 (run \uc0ac\uc6a9\ud558\ub294 \uac83\uc774 \ub0ab\ub2e4.)\nval person = Person(name = \u201cFB\u201d, age = 29, temperature = 36.5f)\nval isPersonSick = with(person) {\ntemperature = 37.2f\nisSick() \/\/ \uc774 \ubd80\ubd84\uc774 return\n}<\/p>\n<h3>let<\/h3>\n<p>\uc218\uc2e0\uac1d\uccb4 \uc774\uc6a9\ud55c \ud6c4 \ub9c8\uc9c0\ub9c9 \uc904\uc774 return \ub41c\ub2e4. run, with\uc640 \ub2ec\ub9ac \uc218\uc2e0\uac1d\uccb4\ub97c it \uc73c\ub85c \uc811\uadfc\ud574\uc57c\ud55c\ub2e4.\nnull check \ud6c4 \ucf54\ub4dc\ub97c \uc2e4\ud589\ud574\uc57c\ud560 \ub54c,\nnullable\ud55c \uc218\uc2e0\uac1d\uccb4\ub97c \ub2e4\ub978 \ud0c0\uc785\uc758 \ubcc0\uc218\ub85c \ubcc0\ud658\ud574\uc57c\ud558\ub294 \uacbd\uc6b0 \uc0ac\uc6a9\nnullable \ud55c \uc218\uc2e0\uac1d\uccb4\ub97c \ub2e4\ub8f0 \ub54c\ub294 let\uc744 \uc0ac\uc6a9\ud574\uc57c\ud55c\ub2e4.<\/p>\n<p>var person: Person? = null\nval isReserved = person?.let { it: Person -&gt;\nreserveMovie(it)\n}<\/p>\n<h3>also<\/h3>\n<p>apply \ucc98\ub7fc \uc218\uc2e0\uac1d\uccb4\ub97c return\ud55c\ub2e4. \ud504\ub85c\ud37c\ud2f0\ub97c \uc138\ud305\ud558\ub294 \uac83 \uc678\uc5d0 \ub2e4\ub978 \uc791\uc5c5\ub3c4 \uac00\ub2a5\nit \uc73c\ub85c \uac1d\uccb4 \uc811\uadfc, \uc798 \uc0ac\uc6a9\uc548\ub428<\/p>\n<p>var number = 3<\/p>\n<p>fun getAndIncrease() = number.also {\nnumber++\n}<\/p>\n<p>getAndIncrease()\ngetAndIncrease()<\/p>\n<p>\/\/ 3\n\/\/ 4<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[11],"tags":[],"class_list":["post-30","post","type-post","status-publish","format-standard","hentry","category-kotlin"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/devserver.kr\/blog\/wp-json\/wp\/v2\/posts\/30","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devserver.kr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devserver.kr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devserver.kr\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/devserver.kr\/blog\/wp-json\/wp\/v2\/comments?post=30"}],"version-history":[{"count":1,"href":"https:\/\/devserver.kr\/blog\/wp-json\/wp\/v2\/posts\/30\/revisions"}],"predecessor-version":[{"id":31,"href":"https:\/\/devserver.kr\/blog\/wp-json\/wp\/v2\/posts\/30\/revisions\/31"}],"wp:attachment":[{"href":"https:\/\/devserver.kr\/blog\/wp-json\/wp\/v2\/media?parent=30"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devserver.kr\/blog\/wp-json\/wp\/v2\/categories?post=30"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devserver.kr\/blog\/wp-json\/wp\/v2\/tags?post=30"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}