kotlin エラー「error: the integer literal does not conform to the expected type Int」の解決方法

kotlin エラー「error: the integer literal does not conform to the expected type Int」の解決方法

kotlinで、エラー「error: the integer literal does not conform to the expected type Int」の解決方法を記述してます。intに対してLong型を指定して代入している場合などに発生します。

環境

  • OS windows11 home
  • java 17.0.2
  • kotlin 1.6.10-release-923

エラー全文

以下のコードで発生。

fun main() {

    var x:Int = 1L

}

エラーメッセージ

error: the integer literal does not conform to the expected type Int
    val i: Int = 1L

原因

「Int」型に、明示的に「Long」型を指定して代入しているため

対処法

「Long」型を使用する

fun main() {

    var x:Long = 1L

}