Hanbit the Developer

Kotlin Documentation | Equality 본문

Mobile/Kotlin

Kotlin Documentation | Equality

hanbikan 2023. 5. 22. 15:42

Kotlin Documentation 시리즈에 대해

Category: Concepts

문서 링크: https://kotlinlang.org/docs/equality.html


  • Structural equality: ==
  • Referential equality: ===

Structural equality

a == b는 다음 코드로 번역된다:

a?.equals(b) ?: (b === null)

equals 함수로 contents(주소값이 아니라)를 비교하며, elvis 연산자를 통해 a가 null인 경우 b 또한 null인지 체크한다.

equals(other: Any?): Boolean을 override함으로써 ==을 커스터마이징 할 수 있다.

Referential equality

주소 체크. primitive의 경우에는 ==와 같은 연산을 하게 된다.

 

*Java에서는 ==가 참조 비교, equals()가 내용 비교이다.(===는 없음)