Java中Integer類主要方法及部分代碼演示

構造方法

Integer(int value) 通過指定的int值構成一個Integer對象
Integer(String s) 通過指定的String值構成一個Integer對象
Integer in=new Integer(5);

其他方法

int intValue() 將此對象轉化爲int
long longValue() 將此對象轉化爲long
byte byteValue() 將此對象轉化爲byte
float floatValue() 將此對象轉化爲float
short shortValue() 將此對象轉化爲short
double doubleValue() 將此對象轉化爲double
Integer in=new Integer(5);
		in.intValue();
String toString() 返回String表示此Integer值的對象
Integer in=new Integer(555);
		String s;
		s=in.toString();
		System.out.print(s);//輸出串555

靜態方法(可直接拿出去用的方法,不用實例化對象)

static Integer valueOf(int i) 將int轉化爲Integer
static Integer valueOf(String s) 將String轉化爲Integer
static Integer valueOf(String s, int radix) 返回一個Integer對象,將radix進制數轉化爲10進制
System.out.println(  Integer.valueOf("100",2));//輸出4
static int max(int a, int b) 返回兩個int值中的較大值,就像通過調用Math.max一樣
static int min(int a, int b) 返回兩個int值中較小的一個,就像通過調用Math.min一樣
static int sum(int a, int b) 根據+運算符將兩個整數相加
static String toBinaryString(int i) 將int轉換爲二進制字符串
System.out.println(  Integer.toBinaryString(5));//輸出5
static String toHexString(int i) 將int轉換爲16進制的字符串
System.out.println(  Integer.toHexString(456)); //1c8
static String toOctalString(int i) 轉換爲8進制字符串
static int lowestOneBit(int i) 返回int二進制的最低位
 System.out.println(Integer.lowestOneBit(5));//輸出1(5的二進制爲101)
static int highestOneBit(int i) 返回int二進制的最高位,其餘爲0
System.out.println(Integer. highestOneBit(5));//輸出4(100)
static int parseInt(String s) 將串變爲10進制數
static int parseInt(String s, int radix) n進制轉10進制 字符串結果
 System.out.println(  Integer.parseInt("10000",2));//輸出16
static int compare(int x, int y) 以int數字方式比較兩個值,參數一大爲1,相等爲0,參數二大爲-1
static int compareUnsigned(int x, int y) 比較兩個int值,以數值方式將值視爲無符號(並非絕對值,而是二進制的無符號)
static String toString(int i) 返回String表示指定整數的對象
static String toString(int i, int radix) 返回第二個參數指定的基數(計算機進制)中第一個參數的字符串表示形式
System.out.println(  Integer.toString(5,2));//輸出101

上面就是Integer類中的主要的方法。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章