0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

はじめに

今回はKotlinのデータ型についてJavaとの比較を交えながらまとめたいと思います。


記事投稿の経緯

Kotlinのデータ型はIntなどが存在するが、Javaにはint,Integerの2パターンが存在する。(プリミティブ型と参照型)

そこで、Kotlinのデータ型がJavaのデータ型とどのように対応しているのかについて疑問を持ち、調査することにした。


実装

IntelliJ IDEAを用いてKotlinコードからJavaコードにDecompileして確認してみる。

Tools > Kotlin > Show Kotlin Bytecode > 「Decompile」ボタンクリック


import kotlin.jvm.JvmStatic

class KotlinDecompile {
    fun foo(): Int { return 1 }

    fun foo2(): Double { return 1.0 }

    fun foo3(): Boolean { return true }

    fun foo4(): Char { return 'a' }

    fun foo5(): Int? { return 1 }

    fun foo6(): Double? { return 1.0 }

    fun foo7(): Boolean? { return true }

    fun foo8(): Char? { return 'a' }

    fun foo9(): Array<String> { return arrayOf("a") }

    fun foo10(): List<String> { return listOf("a") }

    fun foo11(): String { return "a" }

    companion object {
        @JvmStatic
        fun main(args: Array<String>) {
        }
    }
}
import java.util.List;
import kotlin.Metadata;
import kotlin.collections.CollectionsKt;
import kotlin.jvm.JvmStatic;
import kotlin.jvm.internal.DefaultConstructorMarker;
import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public final class KotlinDecompile {
   @NotNull
   public static final KotlinDecompile.Companion Companion = new KotlinDecompile.Companion((DefaultConstructorMarker)null);

   public final int foo() {
      return 1;
   }

   public final double foo2() {
      return 1.0D;
   }

   public final boolean foo3() {
      return true;
   }

   public final char foo4() {
      return 'a';
   }

   @Nullable
   public final Integer foo5() {
      return 1;
   }

   @Nullable
   public final Double foo6() {
      return 1.0D;
   }

   @Nullable
   public final Boolean foo7() {
      return true;
   }

   @Nullable
   public final Character foo8() {
      return 'a';
   }

   @NotNull
   public final String[] foo9() {
      return new String[]{"a"};
   }

   @NotNull
   public final List foo10() {
      return CollectionsKt.listOf(new String[]{"a"});
   }

   @NotNull
   public final String foo11() {
      return "a";
   }

   @JvmStatic
   public static final void main(@NotNull String[] args) {
      Companion.main(args);
   }
}

※今回はデータ型の比較のみを行いたいため、一部コードを省略しています。


まとめ

以下のような関係性になると考えられる。

Kotlin Java
Int int
Double double
Boolean boolean
Char char
Int? Integer
Double? Double
Boolean? Boolean
Char? Character
Array []
List List

参考記事

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?