Kotlin Equality – Difference between “===” vs “==”
Kotlin
88
Male avatar

loveprogramming viết ngày 17/04/2021

https://grokonez.com/kotlin/kotlin-equality-difference-referential-equality-vs-structural-equality

Kotlin Equality – Difference between “===” vs “==”

In the tutorial, Grokonez will introduce Kotlin Equality, the difference between === and ==.

I. Kotlin Equality

We have 2 types of Kotlin equality:

  • Referential equality with === operation
  • Structural equality with == operation

    1. Referential equality

    Referential equality is used to check references with same pointing object. It represents with === operation (negative form !==).

// ###########################
// 1. referential equality
// ###########################
val str = "a"
val str1 = "a"
println(str===str1)
/*
    true
    -> a & b point to the same object
*/

val i = Integer(10)
val j = Integer(10)
println(i===j)
/*
    false
    -> i & j point to the difference objects
*/

val jack = Customer("Jack", 25)
var customer = jack;
println(jack === customer)
/*
    true
    -> Because 'jack' and 'customer' objects point to the same object 
*/  

customer = Customer("Jack", 25)
println(jack === customer)
/*
    false
    -> Because 'jack' and 'customer' objects point to the difference object 
*/

2. Structural equality

Structural equality is used to check equality of objects by equals() function with == operation (negative form !=)

a==b is translated to a?equals(b) ?: (b === null)

More at:

https://grokonez.com/kotlin/kotlin-equality-difference-referential-equality-vs-structural-equality

Kotlin Equality – Difference between “===” vs “==”

Bình luận


White
{{ comment.user.name }}
Bỏ hay Hay
{{comment.like_count}}
Male avatar
{{ comment_error }}
Hủy
   

Hiển thị thử

Chỉnh sửa

Male avatar

loveprogramming

451 bài viết.
79 người follow
Kipalog
{{userFollowed ? 'Following' : 'Follow'}}
Cùng một tác giả
Male avatar
1 0
Tutorial Link: (Link) (Ảnh) Django is a Pythonbased free and opensource web framework that follows the modeltemplateview architectural pattern. A...
loveprogramming viết 6 tháng trước
1 0
Male avatar
1 0
https://loizenai.com/angular11nodejspostgresqlcrudexample/ Angular 11 Node.js PostgreSQL Crud Example (Ảnh) Tutorial: “Angular 11 Node.js Postg...
loveprogramming viết 5 tháng trước
1 0
Male avatar
1 0
Angular Spring Boot jwt Authentication Example Github https://loizenai.com/angularspringbootjwt/ (Ảnh) Tutorial: ” Angular Spring Boot jwt Authe...
loveprogramming viết 5 tháng trước
1 0
Bài viết liên quan
Male avatar
0 0
https://grokonez.com/kotlin/kotlinreadwritecsvfileexample How to read/write CSV file in Kotlin In this tutorial, we're gonna look at examples tha...
loveprogramming viết 7 ngày trước
0 0
Male avatar
0 0
https://grokonez.com/frontend/angular/angular6/kotlinspringbootangular6crudhttpclientmysqlexamplespringdatajparestapisexample Kotlin Spring Boot +...
loveprogramming viết 1 tháng trước
0 0
Male avatar
0 0
https://grokonez.com/kotlin/kotlinmutableimmutablevariablevalvsvar Kotlin mutable & immutable variable – Val vs Var In the tutorial, JavaSampleAp...
loveprogramming viết 28 ngày trước
0 0
{{like_count}}

kipalog

{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}


Male avatar
{{userFollowed ? 'Following' : 'Follow'}}
451 bài viết.
79 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!