Java 中的整数比较
Integer comparison in Java
Java 中的整数比较很棘手,因为 int
和 Integer
的行为不同。我明白了。
但是,正如 example program 所示,(Integer)400
(第 4 行) 的行为不同于 (Integer)5
(第 3 行)。这是为什么??
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
System.out.format("1. 5 == 5 : %b\n", 5 == 5);
System.out.format("2. (int)5 == (int)5 : %b\n", (int)5 == (int)5);
System.out.format("3. (Integer)5 == (Integer)5 : %b\n", (Integer)5 == (Integer)5);
System.out.format("4. (Integer)400 == (Integer)400 : %b\n", (Integer)400 == (Integer)400);
System.out.format("5. new Integer(5) == (Integer)5 : %b\n", new Integer(5) == (Integer)5);
}
}
结果
1. 5 == 5 : true // Expected
2. (int)5 == (int)5 : true // Expected
3. (Integer)5 == (Integer)5 : true // Expected
4. (Integer)400 == (Integer)400 : false // WHAT?
5. new Integer(5) == (Integer)5 : false // Odd, but expected
来自JLS
If the value p being boxed is true, false, a byte, or a char in the range \u0000 to \u007f, or an int or short number between -128 and 127 (inclusive), then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.
Ideally, boxing a given primitive value p, would always yield an identical reference. In practice, this may not be feasible using existing implementation techniques. The rules above are a pragmatic compromise. The final clause above requires that certain common values always be boxed into indistinguishable objects. The implementation may cache these, lazily or eagerly. For other values, this formulation disallows any assumptions about the identity of the boxed values on the programmer's part. This would allow (but not require) sharing of some or all of these references.
This ensures that in most common cases, the behavior will be the desired one, without imposing an undue performance penalty, especially on small devices. Less memory-limited implementations might, for example, cache all char and short values, as well as int and long values in the range of -32K to +32K.
引用自JLS:
If the value p being boxed is true, false, a byte, or a char in the range \u0000 to \u007f, or an int or short number between -128 and 127 (inclusive), then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.
简而言之 Integer
使用池,因此对于从 -128 到 127 的数字,装箱后您将始终获得相同的对象,例如 new Integer(120) == new Integer(120)
将计算为 true
,但 new Integer(130) == new Integer(130)
将评估为 false
.
因为在将文字自动装箱到 Integer
时,计算如下:
(Integer)400 --- Integer.valueOf(400)
valueOf is implemented such that certain numbers are "pooled", and it returns the same instance for values smaller than 128.
并且由于 (Integer)5
小于 128,因此将被合并而 (Integer)400
不会被合并。
因此:
3. (Integer)5 == (Integer)5 : true // Expected -- since 5 is pooled (i.e same reference)
和
4. Integer(400) == (Integer)400 : false // WHAT? -- since 400 is not pooled (i.e different reference)
Java 中的整数比较很棘手,因为 int
和 Integer
的行为不同。我明白了。
但是,正如 example program 所示,(Integer)400
(第 4 行) 的行为不同于 (Integer)5
(第 3 行)。这是为什么??
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
System.out.format("1. 5 == 5 : %b\n", 5 == 5);
System.out.format("2. (int)5 == (int)5 : %b\n", (int)5 == (int)5);
System.out.format("3. (Integer)5 == (Integer)5 : %b\n", (Integer)5 == (Integer)5);
System.out.format("4. (Integer)400 == (Integer)400 : %b\n", (Integer)400 == (Integer)400);
System.out.format("5. new Integer(5) == (Integer)5 : %b\n", new Integer(5) == (Integer)5);
}
}
结果
1. 5 == 5 : true // Expected
2. (int)5 == (int)5 : true // Expected
3. (Integer)5 == (Integer)5 : true // Expected
4. (Integer)400 == (Integer)400 : false // WHAT?
5. new Integer(5) == (Integer)5 : false // Odd, but expected
来自JLS
If the value p being boxed is true, false, a byte, or a char in the range \u0000 to \u007f, or an int or short number between -128 and 127 (inclusive), then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.
Ideally, boxing a given primitive value p, would always yield an identical reference. In practice, this may not be feasible using existing implementation techniques. The rules above are a pragmatic compromise. The final clause above requires that certain common values always be boxed into indistinguishable objects. The implementation may cache these, lazily or eagerly. For other values, this formulation disallows any assumptions about the identity of the boxed values on the programmer's part. This would allow (but not require) sharing of some or all of these references.
This ensures that in most common cases, the behavior will be the desired one, without imposing an undue performance penalty, especially on small devices. Less memory-limited implementations might, for example, cache all char and short values, as well as int and long values in the range of -32K to +32K.
引用自JLS:
If the value p being boxed is true, false, a byte, or a char in the range \u0000 to \u007f, or an int or short number between -128 and 127 (inclusive), then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.
简而言之 Integer
使用池,因此对于从 -128 到 127 的数字,装箱后您将始终获得相同的对象,例如 new Integer(120) == new Integer(120)
将计算为 true
,但 new Integer(130) == new Integer(130)
将评估为 false
.
因为在将文字自动装箱到 Integer
时,计算如下:
(Integer)400 --- Integer.valueOf(400)
valueOf is implemented such that certain numbers are "pooled", and it returns the same instance for values smaller than 128.
并且由于 (Integer)5
小于 128,因此将被合并而 (Integer)400
不会被合并。
因此:
3. (Integer)5 == (Integer)5 : true // Expected -- since 5 is pooled (i.e same reference)
和
4. Integer(400) == (Integer)400 : false // WHAT? -- since 400 is not pooled (i.e different reference)