{"id":2877,"date":"2025-11-25T15:12:40","date_gmt":"2025-11-25T06:12:40","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=2877"},"modified":"2025-11-25T23:00:24","modified_gmt":"2025-11-25T14:00:24","slug":"kotlin-variables-and-constants","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2025\/11\/25\/kotlin-variables-and-constants\/","title":{"rendered":"[Kotlin]\ubcc0\uc218\uc640 \uc0c1\uc218 \/ Variables and Constants"},"content":{"rendered":"\n<p>\ud83d\udc49\ud83c\udffb \uc544\ub798\ub294 \ubcc0\uc218\uc640 \uc0c1\uc218\ub97c \uc0ac\uc6a9\ud574\uc11c \ud654\uba74\uacfc \ud130\ubbf8\ub110\uc5d0 \uac12\uc744 \ucd9c\ub825\ud558\ub294 \uc571\uc785\ub2c8\ub2e4.<br>Below is an app that uses variables and constants to output values \u200b\u200bto the screen and terminal.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \ubcc0\uc218\ub780 \uc5b4\ub5a4 \uac12\uc744 \ub2f4\ub294 \uadf8\ub987\uacfc \uac19\uc73c\uba70 \ucd5c\ucd08\uc5d0 \uac12\uc774 \ud560\ub2f9\ub41c \ud6c4(\ucd08\uae30\ud654\ub77c\uace0 \ud568) \uac12\uc758 \ubcc0\uacbd\uc774 \uac00\ub2a5\uc740 \uac83\uc744 \uc758\ubbf8\ud569\ub2c8\ub2e4.<br>A variable is like a container that holds a value, and it means that the value can be changed after it is initially assigned a value (called initialization).<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb\uc0c1\uc218\ub780 \uc5b4\ub5a4 \uac12\uc744 \ub2f4\ub294 \uadf8\ub987\uacfc \uac19\uc73c\uba70 \ucd5c\ucd08\uc5d0 \uac12\uc774 \ud560\ub2f9\ub41c \ud6c4(\ucd08\uae30\ud654\ub77c\uace0 \ud568) \uac12\uc744 \ubcc0\uacbd \ud560 \uc218 \uc5c6\ub294 \uac83\uc744 \uc758\ubbf8\ud569\ub2c8\ub2e4.<br>A constant is like a container that holds a value, and after a value is initially assigned to it (called initialization), its value cannot be changed.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \ubcc0\uc218\uc640 \uc0c1\uc218 \uc120\uc5b8<br>Declare variables and constants<\/p>\n\n\n\n<p>\u2714\ufe0f Kotlin Compose\uc5d0\uc11c \uc0c1\uc218\ub294 \ub2e4\uc74c\uacfc \uac19\uc740 \ud615\uc2dd\uc73c\ub85c \uc120\uc5b8\ud569\ub2c8\ub2e4.<br>In Kotlin Compose, constants are declared in the following format:<\/p>\n\n\n\n<p>&#8212; \ud0c0\uc785\ucd94\ub860\uc774 \uac00\ub2a5\ud574\uc11c \ud0c0\uc785\uc744 \uc0dd\ub7b5\ud574\uc11c \uc0ac\uc6a9\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<br>Because type inference is possible, you can omit the type and use it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>val \uc0c1\uc218\uba85&#91;constant name] : \ud0c0\uc785&#91;Type] = \uac12&#91;Value]\n\nval \uc0c1\uc218\uba85&#91;constant name] = \uac12&#91;Value]<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f Kotlin Compose\uc5d0\uc11c \ubcc0\uc218 \uc120\uc5b8\uc740 \ub2e4\uc74c\uacfc \uac19\uc740 \ud615\uc2dd\uc73c\ub85c \uc120\uc5b8\ud569\ub2c8\ub2e4.<br>In Kotlin Compose, variable declarations are declared in the following format:<\/p>\n\n\n\n<p>&#8212; \ud0c0\uc785\ucd94\ub860\uc774 \uac00\ub2a5\ud574\uc11c \ud0c0\uc785\uc744 \uc0dd\ub7b5\ud574\uc11c \uc0ac\uc6a9 \ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<br>Because type inference is possible, you can use it by omitting the type.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var \ubcc0\uc218\uba85&#91;variable name] : \ud0c0\uc785&#91;Type] = \uac12&#91;Value]\n\nvar \ubcc0\uc218\uba85&#91;variable name] = \uac12&#91;Value]<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \ubcc0\uc218\uc640 \uc0c1\uc218\uc758 \uc0ac\uc6a9\ubc29\ubc95 \uc608<br>Examples of how to use variables and constants<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \ubcc0\uc218 \/ Variable\nvar countVar2 = 10       \/\/ \ucd08\uae30\ud654 \/ Initialization\n    countVar2 = 10 + 1   \/\/ \uac12\uc758 \ubcc0\uacbd \uac00\ub2a5 \/ Values \u200b\u200bcan be changed\n\n\/\/ \uc0c1\uc218 \/ Constant\nval countLet2:Int\n    countLet2 = 20       \/\/ \ucd08\uae30\ud654 \/ Initialization\n    countLet2 = 20 + 1   \/\/ \uc5d0\ub7ec\ubc1c\uc0dd \/ Error occurred<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb Compose\ub294 \uc544\ub798\uc640 \uac19\uc740 \ucf54\ub4dc\uac00 \uc5d0\ub7ec\ub97c \ubc1c\uc0dd\uc2dc\ud0a4\uc9c0 \uc54a\uc2b5\ub2c8\ub2e4.<br>Compose does not throw an error for code like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>val countVal = countVar + 1 <\/code><\/pre>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb Compose\uc5d0\uc11c\ub294  LaunchedEffect(Unit) {}\ub294 Swift\uc758 init(){}\uacfc \uac19\uc740 \uc5ed\ud560\uc744 \uc218\ud589\ud569\ub2c8\ub2e4.<br>In Compose, LaunchedEffect(Unit) {} acts like Swift&#8217;s init(){}.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \ubcc0\uc218\uc640 \uc0c1\uc218\uc758 \ucd9c\ub825<br>Output of variables and constants<\/p>\n\n\n\n<p>\u2714\ufe0f\ud130\ubbf8\ub110 \ucd9c\ub825 :<br>terminal output<\/p>\n\n\n\n<p>&#8212; \ucd08\uae30\ud654\uc5d0 \ub300\ud55c \ubb38\uc81c\uac00 \uc5c6\uae30 \ub54c\ubb38\uc5d0 println\uc73c\ub85c \ucd9c\ub825\ud558\uba74 \ub429\ub2c8\ub2e4.<br>Since there is no problem with initialization, you can just output it with println.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \uc0c1\uc218\ub098 \ubcc0\uc218\uc774 \uac12\ub9cc \ucd9c\ub825\ud558\ub294 \uacbd\uc6b0  \n\/\/ If only the value of a constant or variable is output\n\nprintln(countVar)\nprintln(countVal)\n\n\/\/ \uc0c1\uc218\ub098 \ubcc0\uc218\ub97c \ubb38\uc790\uc5f4\uacfc \ud568\uaed8 \ucd9c\ub825\ud558\ub294 \uacbd\uc6b0\n\/\/ When printing constants or variables with strings\n\nprintln(\"countVar:${countVar}\")  \/\/ 10\nprintln(\"countVar2: $countVar2\") \/\/ 12\nprintln(\"countVal: $countVal\")   \/\/ 11\nprintln(\"countVal2: $countVal2\") \/\/ 20<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f\ubdf0\uc5d0 \ucd9c\ub825 :<br>output to view<\/p>\n\n\n\n<p>&#8212; \ubdf0\uc5d0\ub294 \ub2e4\uc74c\uacfc \uac19\uc740 \ubc29\uc2dd\uc73c\ub85c \ucd9c\ub825 \ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<br>The view can be output in the following ways:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \ubcc0\uc218\uc640 \uc0c1\uc218\uac00 \ubb38\uc790\uc5f4\uacfc \ud568\uaed8 \ucd9c\ub825\ud558\ub294 \uacbd\uc6b0\nprintln(\"countVar:${countVar}\") \/\/ \ubcc0\uc218 \/ Variable\nprintln(\"countVal:${countVal}\") \/\/ \uc0c1\uc218 \/ Constant\n\n\/\/ \ubcc0\uc218\ub098 \uc0c1\uc218 \uac12\ub9cc \ucd9c\ub825\ud558\ub294 \uacbd\uc6b0\nprintln(countVar) \/\/ \ubcc0\uc218 \/ Variable\nprintln(countVal) \/\/ \uc0c1\uc218 \/ Constant<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \ucf54\ub4dc \/ Code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.freelifemakers.variable\n\nimport android.os.Bundle\nimport androidx.activity.ComponentActivity\nimport androidx.activity.compose.setContent\nimport androidx.activity.enableEdgeToEdge\nimport androidx.compose.foundation.layout.*\nimport androidx.compose.material3.*\nimport androidx.compose.runtime.*\nimport androidx.compose.ui.Modifier\nimport androidx.compose.ui.tooling.preview.Preview\nimport androidx.compose.ui.unit.dp\nimport com.freelifemakers.variable.ui.theme.VariableTheme\n\n\n\/\/  \ubcc0\uc218\uc640 \uc0c1\uc218 \/ Variables and Constants\nclass MainActivity : ComponentActivity() {\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        enableEdgeToEdge()\n        setContent {\n            VariableTheme {\n                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding -&gt;\n                    ContentView(modifier = Modifier.padding(innerPadding))\n                }\n            }\n        }\n    }\n}\n\n@Composable\nfun ContentView(modifier: Modifier = Modifier) {\n    \/\/ val :\n    \/\/ \uc0c1\uc218 \/ constant --&gt; \ucd08\uae30\ud654 \ud6c4 \uac12\uc744 \ubcc0\uacbd \ud560 \uc218 \uc5c6\uc74c.\n    \/\/ constant --&gt; The value cannot be changed after initialization.\n\n    \/\/ var :\n    \/\/ \ubcc0\uc218 \/ variable --&gt; \ucd08\uae30\ud654 \ud6c4\uc5d0\ub3c4 \uac12\uc744 \ubc14\uafc0 \uc218 \uc788\uc74c.\n    \/\/ variable --&gt; The value can be changed even after initialization.\n\n    \/\/  \ubcc0\uc218 \/ Variable\n    var countVar = 10      \/\/ 10\n    var countVar2 = 10\n        countVar2 = 10 + 2 \/\/ 12\n\n    \/\/ \uc0c1\uc218 \/ Constant\n    val countVal = countVar + 1 \/\/ 11\n    val countVal2:Int\n        countVal2 = 20          \/\/ 20\n        \/\/ countVal2 = 1 --&gt; error\n\n    println(\"countVar:${countVar}\")  \/\/ 10\n    println(\"countVar2: $countVar2\") \/\/ 12\n    println(\"countVal: $countVal\")   \/\/ 11\n    println(\"countVal2: $countVal2\") \/\/ 20\n\n   \/\/ println(\"countLet3:${coutLet3}\")\n\n    \/\/ SwiftUI\uc758 onAppear -&gt; Compose\uc5d0\uc11c\ub294 LaunchedEffect \uc0ac\uc6a9 \ud569\ub2c8\ub2e4.\n    \/\/ \uc0ac\uc2e4 \uc5ec\uae30\uc11c\ub294 \uc0ac\uc6a9\ud558\uc9c0 \uc54a\uc544\ub3c4 \uae30\ub2a5 \uad6c\ud604\uc5d0 \uc804\ud600 \ubb38\uc81c \uc5c6\uc2b5\ub2c8\ub2e4.\n\n    \/\/ In SwiftUI's onAppear -&gt; Compose, we use LaunchedEffect.\n    \/\/ In fact, there is no problem implementing the function even if we don't use it here.\n\n    LaunchedEffect(Unit) {\n        println(\"countVal(LaunchedEffect): $countVal\")\n    }\n\n    Column(\n        modifier = modifier\n            .fillMaxSize()\n            .padding(16.dp),\n        verticalArrangement = Arrangement.spacedBy(8.dp)\n    ) {\n\n        Text(\"Hello, world!\")\n\n        \/\/ ====        \ubdf0 \uc548\uc5d0\uc11c \ubcc0\uc218 \ucd9c\ub825 \ubc29\ubc95         ==== \/\/\n        \/\/ ==== How to output variables in a view ==== \/\/\n\n        Text(\"Count Var: $countVar\")\n        Text(\"Count Var2: $countVar2\")\n        Text(\"Count Value: ${countVal}\")\n        Text(\"Count Value2: ${countVal2}\")\n    }\n}\n\n@Preview(showBackground = true)\n@Composable\nfun ContentViewPreview() {\n    VariableTheme {\n        ContentView()\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udc49\ud83c\udffb \uc544\ub798\ub294 \ubcc0\uc218\uc640 \uc0c1\uc218\ub97c \uc0ac\uc6a9\ud574\uc11c \ud654\uba74\uacfc \ud130\ubbf8\ub110\uc5d0 \uac12\uc744 \ucd9c\ub825\ud558\ub294 \uc571\uc785\ub2c8\ub2e4.Below is an app that uses variables and constants to output values \u200b\u200bto the screen and terminal. \ud83d\udc49\ud83c\udffb \ubcc0\uc218\ub780 \uc5b4\ub5a4 \uac12\uc744 \ub2f4\ub294 \uadf8\ub987\uacfc \uac19\uc73c\uba70 \ucd5c\ucd08\uc5d0 \uac12\uc774 \ud560\ub2f9\ub41c \ud6c4(\ucd08\uae30\ud654\ub77c\uace0 \ud568) \uac12\uc758 \ubcc0\uacbd\uc774 \uac00\ub2a5\uc740 \uac83\uc744 \uc758\ubbf8\ud569\ub2c8\ub2e4.A variable is like a container that holds a value, and it means [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,1],"tags":[],"class_list":["post-2877","post","type-post","status-publish","format-standard","hentry","category-kotlin","category-uncategorized","missing-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/2877","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=2877"}],"version-history":[{"count":16,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/2877\/revisions"}],"predecessor-version":[{"id":2898,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/2877\/revisions\/2898"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=2877"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=2877"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=2877"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}