简体   繁体   English

有效的java:q比较(等于与==)

[英]Effective java : q on comparison ( equals versus == )

Effective Java 2e, page 43. 有效的Java 2e,第43页。

TOPIC : check that the field of the argument matches the corresponding field of this object. 主题:检查参数的字段是否与此对象的相应字段匹配。

Some object reference fields may legitimately contain null. 某些对象引用字段可能合法地包含null。 To avoid the possibility of a NullPointerException, use this idiom to compare such fields: 为了避免出现NullPointerException的可能性,请使用此惯用法来比较这些字段:

 (field == null ? o.field == null : field.equals(o.field)) 

This alternative may be faster if field and o.field are often identical: 如果字段和o.field通常相同,则此替代方法可能更快:

 (field == o.field || (field != null && field.equals(o.field))) 

Is he implying that field.equals(o.field) produces same behavior as field == o.field ? 他是否暗示field.equals(o.field)产生与field == o.field相同的行为 Can someone explain how the second alternative works ? 有人可以解释第二种选择是如何工作的吗

(field == o.field || (field != null && field.equals(o.field))) (field == o.field ||(field!= null && field.equals(o.field)))

If they are both null, this will be true. 如果它们都为空,那将是真的。

If field is null, it will short-circuit to false, safely. 如果field为null,则会安全地短路为false。

If o.field is null, it will be the field object's responsibility to check for nullness in o.field , which is fine. 如果o.field为null,则field对象负责检查o.field中的o.field ,这很好。

If they are identical - ie they are the same object, not just equal according to equals() - this will return true and complete quickly. 如果它们是相同的 - 即它们是相同的对象,而不仅仅是等于(等于) - 这将返回真实并快速完成。

If they are both not null and not equal, then it will evaluate to false. 如果它们都不为空且不相等,则它将评估为false。

Is he implying that field.equals(o.field) produces same behavior as field == o.field ? 他是否暗示field.equals(o.field)产生与field == o.field相同的行为?

No of course not, one of the first things Java programmers learn is not to compare Strings with ==, because they are not the same. 当然不是,Java程序员学习的第一件事就是不要将字符串与==进行比较,因为它们不一样。

(field == o.field || (field != null && field.equals(o.field)))

Read verbosely translates to 阅读详细翻译

If both are null or they are interned then they are `equal`. 
If they are not equal and field is not null, 
    Then the strings may have equality even if they are not interned. 
    So do a content check for equality of the strings

Basically what he is saying is, that if string1 == string2 , then ether they are both null or both are not null , If both are not null and are equal, then the strings must be interned . 基本上他所说的是,如果string1 == string2 ,那么ether它们都是null或者两者都不是not null ,如果两者都不是null并且相等,那么必须对这些字符串进行interned However if string1 != string2 then one of there things but be true. 但是如果string1 != string2那么其中一个东西却是真的。

  1. field is null or o.field is null field为null或o.field为null
  2. The strings have different content 字符串有不同的内容
  3. The strings have the same content and they are not interned 字符串具有相同的内容,并且它们不是实体

The advantage of doing this is you don't always have to do a comparison for equality by length, then character by character. 这样做的好处是你不必总是按长度比较平等,然后逐个字符地进行比较。 This can be rather slow if both strings are very, very long. 如果两个字符串非常非常长,这可能会相当慢。

What is String interning? 什么是字符串实习?

Code Example: 代码示例:

public class InternedDemo {

    public static void main(String[] args) {
        String s1 = "hello world";
        String s2 = "hello world";
        String s3 = new String("hello world");

        System.out.println(s1 == s2); // true
        System.out.println(s1 == s3); // false
        System.out.println(s2 == s3); // false

        System.out.println(s1.equals(s3)); // true
        System.out.println(s2.equals(s3)); // true
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM