Sobes.tech
Junior — Middle

Explain the principle of the equality operator (==) in programming language.

sobes.tech AI

Answer from AI

In programming languages, such as Java or Kotlin (commonly used in Android), the equality operator == compares two values.

  • For primitive types (int, boolean, etc.), == compares the actual values.
  • For reference types (objects), == compares references, meaning it checks whether both variables point to the same object in memory.

If you need to compare the contents of objects, you usually use the equals() method, which can be overridden for custom comparison logic.

Example in Kotlin:

val a = "test"
val b = "test"
println(a == b)       // true, because it compares the content of the strings
println(a === b)      // can be true or false, as it compares references

Here, == in Kotlin calls equals(), and === is the reference equality operator.