In Java, when comparing whether 2 objects initialized from a class are equal or not, we need to implement 2 equals() and hashCode() methods for that class and will use the equals() method to compare them.
For example, I have a Student class containing name and age information, defined with equals() and hashCode() methods as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package com.huongdankotlin; import java.util.Objects; public class Student { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Student student = (Student) o; return age == student.age && name.equals(student.name); } @Override public int hashCode() { return Objects.hash(name, age); } } |
To compare objects of Student class, we need to use equals() method instead of the equality operator, for example as follows:
1 2 3 4 5 6 7 8 9 10 11 |
package com.huongdankotlin; public class Application { public static void main(String[] args) { Student student1 = new Student("Khanh", 35); Student student2 = new Student("Khanh", 35); System.out.println(student1 == student2); System.out.println(student1.equals(student2)); } } |
Result:
As you can see, with the equality operator, comparing two Student objects will return false.
However, in Kotlin, if you write the same code as above:
1 2 3 4 5 6 7 8 9 |
package com.huongdankotlin fun main() { var student1 = Student("Khanh", 35) var student2 = Student("Khanh", 35) println(student1 == student2) println(student1.equals(student2)) } |
You will see the following output when you run this code:
Obviously, there is a difference here between Java and Kotlin right? Using the equality operator to compare 2 objects of Student class with the same name and same age in Kotlin still returns true. That’s because, the equality operator in Kotlin is structural equality, as long as 2 objects have the same content, no matter the address of these objects in memory, they will be the same. For this reason, if you use the equals() method to compare two objects in Kotlin, IntelliJ will suggest you use the equality operator to make your code more concise.
To compare reference (referential equality) in Kotlin, you need to write code to compare 2 objects with triple equals as follows:
1 2 3 4 5 6 7 8 |
package com.huongdankotlin fun main() { var student1 = Student("Khanh", 35) var student2 = Student("Khanh", 35) println(student1 === student2) } |
hen, the result when comparing will be the same as Java, always false:
If:
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.huongdankotlin fun main() { var student1 = Student("Khanh", 35) var student2 = Student("Khanh", 35) println(student1 === student2) var student3 = student2 println(student2 === student3) } |
the result of the last line of code will be true:
We have equality comparison and non-equal comparison in Java with “==” and “!=”. In Kotlin we use “===” and “!==”, for example like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.huongdankotlin fun main() { var student1 = Student("Khanh", 35) var student2 = Student("Khanh", 35) println(student1 === student2) var student3 = student2 println(student2 === student3) println(student2 !== student3) } |
Result: