Integer類簡介:
- Integer類是基本數據類型int的包裝器類,是抽象類Number的子類,位於java.lang包中。
- Integer類在對象中包裝了一個基本類型int的值,也就是每個Integer對象包含一個int類型的字段。在Integer源碼中如下定義:private final int value;
字段:
- [static int] MAX_VALUE:值為 231-1 的常量,它表示
int
類型能夠表示的最大值。 - [static int] MIN_VALUE:值為 -231 的常量,它表示
int
類型能夠表示的最小值。 - [static int] SIZE: 用來以二進制補碼形式表示 int 值的比特位數。
- [static Class<Integer>] TYPE:表示基本類型
int
的Class
實例。 - [static int] BYTES:返回int值所占的字節數。
1 System.out.println(Integer.MAX_VALUE); //2147483647 2 System.out.println(Integer.MIN_VALUE); //-2147483648 3 System.out.println(Integer.BYTES); //4 4 System.out.println(Integer.SIZE); //32
構造方法:
Integer類提供了兩種構造方法:它們都會返回一個Integer對象
(1)Integer(int value);
(2)Integer(String s); //要注意的是字符串不能包含非數字字符,否則會拋出NumberFormatException
(3)除此之外,還可以給Integer對象直接賦值,如:Integer a = 10;
1 Integer w = new Integer(100); 2 Integer q = new Integer("100"); 3 4 //Integer a = new Integer("100a"); //這是不合法的
一些重要的方法:
[static int] bitCount(int i) | 返回指定 int 值的二進制補碼表示形式的 1 位的數量 |
[static int] compare(int x,int y) | 比較x和y的值,當x大於y時返回1,當x等於y時返回0,否則返回-1 |
[int] compareTo(Integer anotherInteger) | 在數值上比較兩個Integer對象 |
[boolean] equals(Object obj) | 比較此對象與指定對象 |
[int] hashcode() | 返回此對象的哈希碼 |
[static int] parseInt(String str) | 將字符串參數作為有符號十進制數進行解析 |
[static String] toBinaryString(int i) | 以二進制(基數 2)無符號整數形式返回一個整數參數的字符串表示形式 |
[String] toString() | 返回該Integer值得String對象 |
[static Integer] valueOf(int i) | 返回一個表示指定的 int 值的 Integer 實例 |
方法演示:
1 int a = 5; 2 Integer b = 5; 3 Integer c = 15; 4 5 //我們來看一下5的低八位:0000 0101,而前面24位都是0,所以它的1的個數是2 6 System.out.println(Integer.bitCount(a)); //2 7 8 //由於3大於2,所以返回值是1 9 System.out.println(Integer.compare(3, 2)); //1 10 11 //由於b=5小於c=15,所以返回-1 12 System.out.println(b.compareTo(c)); //-1 13 14 //將字符串“110”轉換成數字110 15 int d = Integer.parseInt("110"); 16 System.out.println(d); //110 17 d = d+Integer.valueOf(10); 18 System.out.println(d); //120
特別注意:
(1)“==”和equals()方法:在Integer類中,“==”用來比較對象地址是否相同,並且Integer類重寫了equals(Object obj)方法,在equals(Object obj)方法中,會先判斷參數中的對象obj是否是Integer類型的對象,如果是則判斷值是否相同,值相同則返回true,值不同則返回false,如果obj不是Integer類的對象,則返回false。需要注意的是:當參數是基本類型int時,編譯器會給int自動裝箱成Integer類,然后再進行比較。
1 //Integer類中的equals(Object obj)源代碼 2 3 public boolean equals(Object obj) { 4 if (obj instanceof Integer) { 5 return value == ((Integer)obj).intValue(); 6 } 7 return false; 8 }
1 //int和Integer對象用構造方法、直接賦值方法賦值比較 2 int a = 200; 3 Integer b = new Integer(200); 4 Integer c = 200; 5 6 //b和c都是Integer對象,所以用“==”比較時,就是比較內存地址值是否相等 7 System.out.println(b == c); //false 8 System.out.println(b.equals(c)); //true 9 10 //a在進行比較時,會自動裝箱 11 System.out.println(b.equals(a)); //true 12 13 //因為b是Integer對象,a是基本數據類型int的變量,當進行“==”比較時,b會自動拆箱成int,從而比較的就是變量的值是否相等 14 15 System.out.println(b == a); //true
(2)Integer直接賦值和valueOf()方法,下面來看下面兩段代碼的不同:
1 Integer i02 = 59; 2 Integer i03 = Integer.valueOf(59); 3 Integer i04 = new Integer(59); 4 5 System.out.println(i02 == i03); //true 6 System.out.println(i02 == i04); //false 7 System.out.println(i03 == i04); //false
1 Integer i02 = 200; 2 Integer i03 = Integer.valueOf(200); 3 Integer i04 = new Integer(200); 4 5 System.out.println(i02 == i03); //false 6 System.out.println(i02 == i04); //false 7 System.out.println(i03 == i04); //false
- 我們可以看到上面兩段代碼只是賦給對象的值不同,但是在判斷”i02 == i03“時得到的結果卻不同,這是為何:當使用直接賦值如”Integer i01 = 59“的時候,會調用Integer的valueOf()方法,這個方法就是返回一個Integer對象,但是在返回前,作了一個判斷,判斷要賦給對象的值i是否在[-128,127]區間中,且IntegerCache(是Integer類的內部類,里面有一個Integer對象數組,用於存放已經存在的且范圍在[-128,127]中的對象)中是否存在此對象,如果存在,則直接返回引用,否則,創建一個新對象返回。那么我們就可以知道,200這個數字不在[-128,127]中,所以會直接創建一個新對象返回,i02和i03就是兩個不同的對象。而59屬於[-128,127]中,當創建i03時,會直接返回引用,此時i02和i03都指向同一個地址。
- 以第一段代碼為例:”Integer i02 = 59":因為程序初次運行,沒有59,所以直接創建一個對象返回;“Integer i03 = Integer.valueOf(59)”:因為IntegerCache中已經存在59,所以直接返回引用;“Integer i04 = new Integer(59)”:直接創建一個新對象。
- JVM中一個字節一下的整型數據(即[128,127])會在JVM啟動時加載進內存,除非用new Integer()顯示的創建對象,否則都是同一對象。
1 //Integer類中的valueOf()源代碼 2 3 public static Integer valueOf(int i) { 4 if (i >= IntegerCache.low && i <= IntegerCache.high) 5 return IntegerCache.cache[i + (-IntegerCache.low)]; 6 return new Integer(i); 7 }