Equality Operators (==, !=) and Referential equality Operators...
On this page (8sections)
Definition
- The equality operators are operators that check if the values that are compared are equal. This is similar to the Java equals() method. The equality operators are ”==” and ”!=”
- The referential equality operators are operators that check if the values and the object of the variables that are compared are equal. The referential equality operators are ”===” and ”!==“
Syntax
1. Equality Operators
if(value1 == value2){
//Block of Code
}
(OR)
if(value1 != value2){
//Block of Code
}
For example,
var num1 = 5
var num2 = 5
if(num1 == num2){
//Block of Code
}
2. Referential Equality Operators
//value1 and value2 are of different Object types
if(value1 === value2){
//Block of Code
}
(OR)
if(value1 !== value2){
//Block of Code
}
For example,
var num1 : Any = 5
var num2 = 5
if(num1 === num2){
//Block of Code
}
Equality and Referential Equality Operator Example Program in Kotlin
//Equality and Referential Equality Operator Example Program in Kotlin
//Operator Kotlin Programs, Basic Kotlin Program
fun main(args: Array<String>) {
var num1 : Any = 100.2
var num2 = 100.2
var num3 = 100
var num4 : Int = 100
//Checking Equality of same value and non-mentioned Object type
if(num1 == num2){
println("$num1('Any' Data type) and $num2(Inferred Data Type) are equal using Equality Operator")
}
//Checking Non-Equality of different values and non-mentioned Object type
if(num1 != num3){
println("$num1('Any' Data type) and $num3(Inferred Data Type) are not equal using Non-Equality Operator")
}
//Checking Referential Equality of same value and non-mentioned Object type
if(num3 === num4){
println("$num3(Inferred Data Type) and $num4('Int' Data Type) are equal using Referential Equality Operator")
}
//Checking Referential Non-Equality of same value with boxed Object
if(num1 !== num2){
println("$num1('Any' Data Type) and $num2(Inferred Data Type) are not equal using Referential Non-Equality Operator")
}
}
Sample Output
100.2('Any' Data type) and 100.2(Inferred Data Type) are equal using Equality Operator
100.2('Any' Data type) and 100(Inferred Data Type) are not equal using Non-Equality Operator
100(Inferred Data Type) and 100('Int' Data Type) are equal using Referential Equality Operator
100.2('Any' Data Type) and 100.2(Inferred Data Type) are not equal using Referential Non-Equality Operator
How It Works
This Kotlin program demonstrates Equality Operators (==, !=) and Referential equality Operators… It first prepares the data it needs, then uses conditional logic to decide the result, and finally prints the output shown in the Sample Output above.
- Declare the variables that hold the program’s data.
- Use conditional statements to handle the different cases.
- Print the final result to the console so you can compare it with the sample output.
Try changing the input values and re-running the program to see how the output changes — this is the fastest way to understand how the logic behaves.
Frequently Asked Questions
What is Equality Operators (==, !=) and Referential equality Operators... in Kotlin?
The equality operators are operators that check if the values that are compared are equal.
How do I run this Kotlin example?
Run it in IntelliJ IDEA or Android Studio, or compile from the command line with `kotlinc file.kt -include-runtime -d file.jar` and run `java -jar file.jar`.
How can I practice Equality Operators (==, !=) and Referential equality Operators...?
Copy the example into IntelliJ IDEA or Android Studio, run it, then change the values or add print statements to see how the output changes.