kotlin 警告「warning: identity equality for arguments of types Int and Int is deprecated」の解決方法

kotlin 警告「warning: identity equality for arguments of types Int and Int is deprecated」の解決方法

kotlinで、警告「warning: identity equality for arguments of types Int and Int is deprecated」の解決方法を記述してます。数値に対して参照同一性(===)を使用することは非推奨となっているため発生します。

環境

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

警告全文

以下のコードで発生。

fun main() {

    var x:Int = 5
    var y:Int = 5

    println(x === y) // true

}

警告メッセージ

warning: identity equality for arguments of types Int and Int is deprecated
    println(x === y) // true
            ^

原因

数値同士の参照同一性(===)の比較は非推奨なため

対処法

「==」が「equals」を使用する

fun main() {

    var x:Int = 5
    var y:Int = 5

    println(x == y) // true
    println(x.equals(y)) // true

}