再识Integer

再识Integer

Integer

Integer是对象,int是基本类型

1
2
3
4
5
6
7
Integer integer = Integer.valueOf(100);
Integer integer5= Integer.valueOf(100);
Integer integer1 = Integer.valueOf(101);
Integer integer2 = new Integer(100);
Integer integer4 = new Integer(100);
Integer integer3 = new Integer(101);
Integer i = 100;

Integer.valueOf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;

cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);

// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
1
2
3
4
5
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}

Integer.valueOf()返回一个Integer对象,Integer底层维护了一个cache,因为我们常用的数值是处于-128~127之间的Java为了不反复创建对象,会从这个cache中返回,也就是说我连续创建两个Integer.valueOf(100),他们其实返回的是同一个,即:

1
integer == integer5    // true
1
2
i == integer5          // true
// ==100 和valueOf()都是干了同样一件事都是从cache里面取得
1
2
3
Integer i = 100;
int a = 100;
i == a // true Integer和int进行比较时会自动拆包为int然后进行比较

==和equals

== 是直接比较值

1
2
Integer integer = Integer.valueOf(100);
Integer integer5= Integer.valueOf(100);

integer == integer5 因为valueOf从cache里面返回得是同一个值

1
2
3
Integer integer2 = new Integer(100);
Integer i = 100;
i == integer2 // false

==100 和valueOf()都是干了同样一件事都是从cache里面取的,Integer2开辟一段存储空间,里面保存的值是new Integer(100)的地址,new Integer(100)是在堆中创建的明显地址值不一样

1
2
3
Integer c = 100;
int a = 100;
a=c // true

Integer 和int进行比较时会进行拆包转换为int然后进行比较。

equals是Object的方法,所有的类都有这个方法,他的底层重写逻辑也是通过 == 号实现的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {

private Integer userId;

private String userName;

@Override
public boolean equals(Object o) {
// 是同一个引用直接返回true
if (this == o) return true;
// 为空或者不是一个class直接返回false
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
// 判断具体各个属性值是否相等,Integer String均重写了equals方法
return userId.equals(user.userId) && userName.equals(user.userName);
}

@Override
public int hashCode() {
return Objects.hash(userId, userName);
}
}
1
2
3
4
5
6
7
8
// Integer的
public boolean equals(Object obj) {
if (obj instanceof Integer) {
// 也是通过intValue()
return value == ((Integer)obj).intValue();
}
return false;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// String
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}

compare

我们要比较两个Integer可以通过

1
2
3
public static int compare(int x, int y) {
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}

他返回的是一个int类型,可以根据返回的int直接判断大小 == 0 ==-1 ==1

intValue()

通过将integer.intValue()方法转变为int,然后再比较两个Integer


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!