For future students on the course "Kotlin Backend Developer" we prepared a translation of useful material.
We also invite you to watch the open lesson on the topic "Kotlin multiplatform: Front / Back in one language".
There are three ways to check for equality in Kotlin language:
The first way is to compare structures (==)
Operator ==
in Kotlin allows you to compare data contained in variables. However, in Java, this operator is used to compare references of two variables.
In the case of custom classes, ==
it can be used to compare the contents of data classes. Otherwise, this operator compares references.
The second way is comparing links (===)
An operator ===
in Kotlin is used to compare references of two variables. However, in the case of primitives, it ===
is equivalent ==
, that is, it checks the values.
The third way is the method equals
This method equals
performs the same function in Kotlin as ==
.
equals
==
Float
Double
. Float
Double
==
IEEE 754, equals
, equals
, :
NaN ;
NaN , ,
POSITIVE_INFINITY
;
0,0 , 0,0.
, , .
1.
val firstInt = 5
val secondInt = 5
println(firstInt == secondInt) // true
println(firstInt === secondInt) // true
println(firstInt.equals(secondInt)) // true
.
2.
val firstInt = Integer(5)
val secondInt = Integer(5)
println(firstInt == secondInt) // true
println(firstInt === secondInt) // false
println(firstInt.equals(secondInt)) // true
firstInt
secondInt
. (===
) false
. equals
. , true
, 5
.
3.
class Student(val name : String)
val student1 = Student(“Jasmeet”)
val student2 = Student(“Jasmeet”)
println(student1 === student2) // false
println(student1 == student2) // false
println(student1.equals(student2)) // false
println(student1.name === student2.name) // true
println(student1.name == student2.name) // true
println(student1.name.equals(student2.name)) // true
student
, , , . , Java.
, data-.
data class Student(val name : String)
val student1 = Student(“Jasmeet”)
val student2 = Student(“Jasmeet”)
println(student1 === student2) // false
println(student1 == student2) // true
println(student1.equals(student2)) // true
4.
val negativeZero = -0.0f
val positiveZero = 0.0f
println(negativeZero == positiveZero) // true
println(negativeZero.equals(positiveZero)) // false
, ==
IEEE 754. true
. equals
, false
.
: https://kotlinlang.org/docs/reference/equality.html
"Kotlin multiplatform: Front/Back ".