补充(equals比较)
java.lang.String类中的方法
equals判断相等依据
策略:如果与目标相等返回0,小于目标返回值小于0,大于目标返回值大于0
1@Native static final byte LATIN1 = 0; 2@Native static final byte UTF16 = 1;
1 /** 2 * Compares this string to the specified object. The result is {@code 3 * true} if and only if the argument is not {@code null} and is a {@code 4 * String} object that represents the same sequence of characters as this 5 * object. 6 * 7 * <p>For finer-grained String comparison, refer to 8 * {@link java.text.Collator}. 9 * 10 * @param anObject 11 * The object to compare this {@code String} against 12 * 13 * @return {@code true} if the given object represents a {@code String} 14 * equivalent to this string, {@code false} otherwise 15 * 16 * @see #compareTo(String) 17 * @see #equalsIgnoreCase(String) 18 */ 19public boolean equals(Object anObject) { 20 if (this == anObject) { 21 return true; 22 } 23 if (anObject instanceof String) { 24 String aString = (String)anObject; 25 if (coder() == aString.coder()) { 26 return isLatin1() ? StringLatin1.equals(value, aString.value) 27 : StringUTF16.equals(value, aString.value); 28 } 29 } 30 return false; 31} 32 33private boolean isLatin1() { 34 return COMPACT_STRINGS && coder == LATIN1; 35} 36 37###StringLatin1.equals| 38@HotSpotIntrinsicCandidate 39 public static boolean equals(byte[] value, byte[] other) { 40 if (value.length == other.length) { 41 for (int i = 0; i < value.length; i++) { 42 if (value[i] != other[i]) { 43 return false; 44 } 45 } 46 return true; 47 } 48 return false; 49 } 50 51###StringUTF16.equals| 52@HotSpotIntrinsicCandidate 53 public static boolean equals(byte[] value, byte[] other) { 54 if (value.length == other.length) { 55 int len = value.length >> 1; 56 for (int i = 0; i < len; i++) { 57 if (getChar(value, i) != getChar(other, i)) { 58 return false; 59 } 60 } 61 return true; 62 } 63 return false; 64 }
