๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๋นˆ ๊ตฌ๋ฉ ์ฑ„์šฐ๊ธฐ

[Kotlin] const val

์ถœ์ฒ˜

https://thinking-face.tistory.com/185

 

[Kotlin] const, val, var

Kotlin์—์„œ ``const``๋Š” ์ปดํŒŒ์ผ ์‹œ๊ฐ„์— ๊ฒฐ์ •๋œ๋‹ค๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ–๊ณ  ์žˆ๋‹ค. ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰๋˜๋Š” ๋Ÿฐํƒ€์ž„ ์ด์ „์— ์ด๋ฏธ ๊ฒฐ์ •๋˜์–ด ๋ณ€ํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋ž˜์„œ ``const val``์€ ์ปดํŒŒ์ผ ์‹œ๊ฐ„์— ๊ฒฐ์ •๋˜๋Š” ์ƒ์ˆ˜

thinking-face.tistory.com

https://kotlinlang.org/docs/properties.html#compile-time-constants

 

Properties | Kotlin

 

kotlinlang.org

https://stackoverflow.com/questions/37595936/what-is-the-difference-between-const-and-val

 

What is the difference between "const" and "val"?

I have recently read about the const keyword, and I'm so confused! I can't find any difference between const and the val keyword, I mean we can use both of them to make an immutable variable, is th...

stackoverflow.com

 


const ์ˆ˜์ •์ž

-> ์ปดํŒŒ์ผ ํƒ€์ž„ ์ƒ์ˆ˜. ์ปดํŒŒ์ผ ์‹œ๊ฐ„์— ์ฝ๊ธฐ ์ „์šฉ์œผ๋กœ ๊ฐ’์ด ํ• ๋‹น๋œ๋‹ค.

 

  • ์ตœ์ƒ๋‹จ ํ”„๋กœํผํ‹ฐ, object ์„ ์–ธ์˜ ๋ฉค๋ฒ„, companion object์˜ ๋ฉค๋ฒ„๋กœ๋งŒ ์“ธ ์ˆ˜ ์žˆ๋‹ค.
  • ์›์‹œ ํƒ€์ž…๊ณผ String ํƒ€์ž…์œผ๋กœ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.
  • ์ปค์Šคํ…€ ๊ฒŒํ„ฐ ๊ตฌํ˜„์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.

 

์˜ˆ์‹œ

1. ์ตœ์ƒ๋‹จ

์ฝ”ํ‹€๋ฆฐ ์ฝ”๋“œ

val apple = "apple"
const val pear = "pear"

 

-> ์ž๋ฐ” ๋ณ€ํ™˜ ์ฝ”๋“œ

@Metadata(
   mv = {1, 7, 0},
   k = 2,
   d1 = {"\u0000\n\n\u0000\n\u0002\u0010\u000e\n\u0002\b\u0004\"\u0014\u0010\u0000\u001a\u00020\u0001X\u0086D¢\u0006\b\n\u0000\u001a\u0004\b\u0002\u0010\u0003\"\u000e\u0010\u0004\u001a\u00020\u0001X\u0086T¢\u0006\u0002\n\u0000¨\u0006\u0005"},
   d2 = {"apple", "", "getApple", "()Ljava/lang/String;", "pear", "MyCodeForCodingTest"}
)
public final class FruitKt {
   @NotNull
   private static final String apple = "apple";
   @NotNull
   public static final String pear = "pear";

   @NotNull
   public static final String getApple() {
      return apple;
   }
}

 

2. object

์ฝ”ํ‹€๋ฆฐ ์ฝ”๋“œ

object Orange {
    val pineapple = "pineapple"
    const val cherry = "cherry"
}

 

-> ์ž๋ฐ” ๋ณ€ํ™˜ ์ฝ”๋“œ

// Orange.java
import kotlin.Metadata;
import org.jetbrains.annotations.NotNull;

@Metadata(
   mv = {1, 7, 0},
   k = 1,
   d1 = {"\u0000\u0014\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0002\b\u0004\bÆ\u0002\u0018\u00002\u00020\u0001B\u0007\b\u0002¢\u0006\u0002\u0010\u0002R\u000e\u0010\u0003\u001a\u00020\u0004X\u0086T¢\u0006\u0002\n\u0000R\u0014\u0010\u0005\u001a\u00020\u0004X\u0086D¢\u0006\b\n\u0000\u001a\u0004\b\u0006\u0010\u0007¨\u0006\b"},
   d2 = {"LOrange;", "", "()V", "cherry", "", "pineapple", "getPineapple", "()Ljava/lang/String;", "MyCodeForCodingTest"}
)
public final class Orange {
   @NotNull
   private static final String pineapple;
   @NotNull
   public static final String cherry = "cherry";
   @NotNull
   public static final Orange INSTANCE;

   @NotNull
   public final String getPineapple() {
      return pineapple;
   }

   private Orange() {
   }

   static {
      Orange var0 = new Orange();
      INSTANCE = var0;
      pineapple = "pineapple";
   }
}

 

3. companion object

์ฝ”ํ‹€๋ฆฐ ์ฝ”๋“œ

class Grape {
    val banana = "banana"
//    const val plum = "plum" ์„ ์–ธ ๋ถˆ๊ฐ€

    companion object {
        val watermelon = "watermelon"
        const val mango = "mange"
    }
}

 

-> ์ž๋ฐ” ๋ณ€ํ™˜ ์ฝ”๋“œ

// Grape.java
import kotlin.Metadata;
import kotlin.jvm.internal.DefaultConstructorMarker;
import org.jetbrains.annotations.NotNull;

@Metadata(
   mv = {1, 7, 0},
   k = 1,
   d1 = {"\u0000\u0014\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0002\b\u0004\u0018\u0000 \u00072\u00020\u0001:\u0001\u0007B\u0005¢\u0006\u0002\u0010\u0002R\u0014\u0010\u0003\u001a\u00020\u0004X\u0086D¢\u0006\b\n\u0000\u001a\u0004\b\u0005\u0010\u0006¨\u0006\b"},
   d2 = {"LGrape;", "", "()V", "banana", "", "getBanana", "()Ljava/lang/String;", "Companion", "MyCodeForCodingTest"}
)
public final class Grape {
   @NotNull
   private final String banana = "banana";
   @NotNull
   private static final String watermelon = "watermelon";
   @NotNull
   public static final String mango = "mange";
   @NotNull
   public static final Companion Companion = new Companion((DefaultConstructorMarker)null);

   @NotNull
   public final String getBanana() {
      return this.banana;
   }

   @Metadata(
      mv = {1, 7, 0},
      k = 1,
      d1 = {"\u0000\u0014\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0002\b\u0004\b\u0086\u0003\u0018\u00002\u00020\u0001B\u0007\b\u0002¢\u0006\u0002\u0010\u0002R\u000e\u0010\u0003\u001a\u00020\u0004X\u0086T¢\u0006\u0002\n\u0000R\u0014\u0010\u0005\u001a\u00020\u0004X\u0086D¢\u0006\b\n\u0000\u001a\u0004\b\u0006\u0010\u0007¨\u0006\b"},
      d2 = {"LGrape$Companion;", "", "()V", "mango", "", "watermelon", "getWatermelon", "()Ljava/lang/String;", "MyCodeForCodingTest"}
   )
   public static final class Companion {
      @NotNull
      public final String getWatermelon() {
         return Grape.watermelon;
      }

      private Companion() {
      }

      // $FF: synthetic method
      public Companion(DefaultConstructorMarker $constructor_marker) {
         this();
      }
   }
}