官术网_书友最值得收藏!

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
主站蜘蛛池模板: 芦溪县| 包头市| 武夷山市| 蓝田县| 木兰县| 临汾市| 宿松县| 东乡族自治县| 丹棱县| 通渭县| 招远市| 巴中市| 兴业县| 怀仁县| 乐都县| 静安区| 锦州市| 珠海市| 太仆寺旗| 庆城县| 合江县| 呼和浩特市| 陈巴尔虎旗| 阿坝县| 颍上县| 扶沟县| 高雄县| 泾川县| 伊春市| 凭祥市| 清丰县| 和龙市| 太康县| 巍山| 边坝县| 玉林市| 达州市| 正阳县| 达拉特旗| 永济市| 黄浦区|