- Hands-On Design Patterns with Kotlin
- Alexey Soshin
- 105字
- 2021-06-25 20:49:23
Comparison
We were taught very early in Java that comparing objects using == won't produce the expected results, since it tests for reference equality, and we need to use equals() for that.
JVM does string interning to prevent that in some basic cases, so for the sake of the example we'll use new String() to avoid that:
String s1 = "ABC";
String s2 = new String(s1);
System.out.println(s1 == s2); // false
Kotlin translates == to equals():
val s1 = "ABC"
val s2 = String(s1.toCharArray())
println(s1 == s2) // true
If you do want to check for reference equality, use ===:
println(s1 === s2) // false
推薦閱讀
- Implementing Modern DevOps
- The Modern C++ Challenge
- 新一代通用視頻編碼H.266/VVC:原理、標準與實現
- Learning Elixir
- Instant 960 Grid System
- 網頁設計與制作教程(HTML+CSS+JavaScript)(第2版)
- 精通軟件性能測試與LoadRunner實戰(第2版)
- Python應用輕松入門
- JavaScript+Vue+React全程實例
- 深度學習:算法入門與Keras編程實踐
- QGIS By Example
- 程序是怎樣跑起來的(第3版)
- CoffeeScript Application Development Cookbook
- LabVIEW虛擬儀器入門與測控應用100例
- R用戶Python學習指南:數據科學方法