补充关于equals的比较方式

补充(equals比较)

Object中的equals比较的是地址

1 public boolean equals(Object obj) { 2 return (this == obj); 3}

java.lang.String类中equals的方法

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 }

注:更细粒度的equals比较,先比较地址是否相同,看其是否为空,最后看其编码值比较

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java采坑之路

判断相等字符串判断相等        String str1  null;        String str2  "java金融";       // str1.equals(str2);  错误的写法        str2.equals(str1); // 常量写前面        Objects.equ

java中字符串相等判断

字符串的判断有2种:        1、判断地址是否相等 用:        2、判断值是否相等 用:equals方法Object类作为所有类的超类,而Object类的equals方法是直接比较地址的,源码如下:publicbooleanequals(Objectobj){