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】KClassからClassを取得する3通りの方法

Posted at

KClassにはClassを返すプロパティとして以下の3種類が定義されています。

  • .java
  • .javaObjectType
  • .javaPrimitiveType

KotlinInt型を例にすると、返却内容は以下のようになります。

.java .javaObjectType .javaPrimitiveType
Int::class int.class Integer.class int.class
Integer::class Integer.class Integer.class int.class
その他 *.class *.class null

どんな時に気にすべきか

プリミティブ型とラッパー型を区別する必要の有無によって使い分ける必要が出るでしょう。
また、「Classを要求するような実装でプリミティブ型に対応していない」というパターンを踏んだことが有るので、そういった場合には.javaObjectType呼び出しを行う必要が有ります。

参考にさせて頂いた記事

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?