|
基本类型 |
占用空间(Byte) |
表示范围 |
包装器类型 |
|
boolean |
1/8 |
true|false |
Boolean |
|
char |
2 |
-128~127 |
Character |
|
byte |
1 |
-128~127 |
Byte |
|
short |
2 |
-2ˆ15~2ˆ15-1 |
Short |
|
int |
4 |
-2ˆ31~2ˆ31-1 |
Integer |
|
long |
8 |
-2ˆ63~2ˆ63-1 |
Long |
|
float |
4 |
-3.403E38~3.403E38 |
Float |
|
double |
8 |
-1.798E308~1.798E308 |
Double |
@Test public void boxingTest() { Integer i1 = 17; Integer i2 = 17; Integer i3 = 137; Integer i4 = 137; System.out.println(i1 == i2);11 System.out.println(i3 == i4); }false /** * Returns an {@code Integer} instance representing the specified * {@code int} value. If a new {@code Integer} instance is not * required, this method should generally be used in preference to * the constructor {@link #Integer(int)}, as this method is likely * to yield significantly better space and time performance by * caching frequently requested values. * * This method will always cache values in the range -128 to 127, * inclusive, and may cache other values outside of this range. * * @param i an {@code int} value. * @return an {@code Integer} instance representing {@code i}. * @since 1.5 */ public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); } @Test public void unboxingTest() { Integer i1 = 17; int i2 = 17; int i3 = 137; Integer i4 = 137; System.out.println(i1 == i2);10 System.out.println(i3 == i4); }truetrue /** * Returns the value of this {@code Integer} as an * {@code int}. */ public int intValue() { return value; } @Test public void unboxingTest() { Integer i1 = 17; Integer i2 = 17; Integer i3 = 137; Integer i4 = 137; // == System.out.println(i1 == i2); System.out.println(i3 == i4); // equals System.out.println(i1.equals(i2));15 System.out.println(i3.equals(i4)); }truefalsetruetrue /** * Compares this object to the specified object. The result is * {@code true} if and only if the argument is not * {@code null} and is an {@code Integer} object that * contains the same {@code int} value as this object. * * @param obj the object to compare with. * @return {@code true} if the objects are the same; * {@code false} otherwise. */ public boolean equals(Object obj) { if (obj instanceof Integer) { return value == ((Integer)obj).intValue(); } return false; }public static void main(String args[]) { Integer a = 1; Integer b = 2; Integer c = 3; Integer d = 3; Integer e = 321; Integer f = 321; Long g = 3L; Long h = 2L; System.out.println(c==d); // 会自动拆箱后再自动装箱 System.out.println(e==f); // 虽然“==”比较的是引用的是否是同一对象,但这里有算术运算,如果该引用为包装器类型则会导致自动拆箱 System.out.println(c==(a+b));// equals 比较的是引用的对象的内容(值)是否相等,但这里有算术运算,如果该引用为包装器类型则会导 System.out.println(c.equals(a+b)); // 首先a+b触发自动拆箱后值为int型,所以比较的是值是否相等 System.out.println(g==(a+b)); // 首先a+b触发自动拆箱后值为int型,自动装箱后为Integer型,然后g为Long型 System.out.println(g.equals(a+b));// 首先a+h触发自动拆箱后值为long型,因为int型的a会自动转型为long型的g然后自动装箱后为Long型, // 而g也为Long型 System.out.println(g.equals(a+h)); }truefalsetruetruetruefalsetrue原文:http://www.cnblogs.com/wuyun-blog/p/4783563.html