int 和 Integer 的區別與作用

2021-07-13 18:47:43 字數 3018 閱讀 1472

char character

byte byte

short short

int integer

long long

float float

double double

引用型別和原始型別的行為完全不同,並且它們具有不同的語義。引用型別和原始型別具有不同的特徵和用法,它們包括:大小和速度問題,這種型別以那種型別的資料結構儲存,當引用型別和原始型別用作某個類的例項時所指定的預設值。物件引用例項變數的預設值為null,而原始型別例項變數的預設值與它們的型別有關。

integer類的使用方法

1、屬性。

static int max_value:返回最大的整型數;

static int min_value:返回最小的整型數;

static class type :返回當前型別。

例子**:

system.out.println(「integer.max_value: 」 + integer.max_value );

結果為:integer.max_value: 2147483647

2、建構函式。

integer(int value) :通過乙個int的型別構造物件;

integer(string s) :通過乙個string的型別構造物件;

例子**:

integer i = new integer(「1234」);

生成了乙個值為1234的integer物件。

3、方法。

說明:

1. 所有方法均為public;

2. 書寫格式:[修飾符] 《返回型別》 《方法名([引數列表])>

如: static int parseint(string s) 表示:此方法(parseint)為類方法(static),返回型別為(int),方法所需引數為string型別。

1. bytevalue():取得用byte型別表示的整數;

2. int compareto(integer anotherinteger) :比較兩個整數。相等時返回0;小於時返回負數;大於時返回正數。

例子**:

integer i = new integer(1234);

system.out.println(「i.compareto: 」 + i.compareto(new integer(123)) );

結果為:i.compareto: 1

3. int compareto(object o) :將該整數與其他類進行比較。如果o也為integer類,進行方法2的操作;否則,丟擲classcastexception異常。

4. static integer decode(string nm) :將字串轉換為整數。

5. double doublevalue() :取得該整數的雙精度表示。

6. boolean equals(object obj) :比較兩個物件。

7. float floatvalue() :取得該整數的浮點數表示。

8. static integer getinteger(string nm) :根據指定名確定系統特徵值。

9. static integer getinteger(string nm, int val) :上面的過載。

10. static integer getinteger(string nm, integer val) :上面的過載。

11. int hashcode() :返回該整數型別的雜湊表碼。

12. int intvalue() : 返回該整型數所表示的整數。

13. long longvalue() :返回該整型數所表示的長整數。

14. static int parseint(string s) :將字串轉換成整數。s必須是時進製數組成,否則丟擲numberformatexception異常。

15. static int parseint(string s, int radix) :以radix為基數radix返回s的十進位制數。所謂的基數就是「幾進製」。

例子**:

string s1 = new string(「1010」);

system.out.println(「integer.parseint(string s, int radix): 」 + integer.parseint(s1,2) );

結果為:integer.parseint(string s, int radix): 10

即s1在此例中被看成是二進位制,返回十進位制就是10。

16. short shortvalue() :返回該整型數所表示的短整數。

17. static string tobinarystring(int i) :將整數轉為二進位制數的字串。

18. static string tohexstring(int i) :將整數轉為十六進製制數的字串。

19. static string tooctalstring(int i) :將整數轉為八進位制數的字串。

20. string tostring() :將該整數型別轉換為字串。

21. static string tostring(int i) :將該整數型別轉換為字串。不同的是,此為類方法。

22. static string tostring(int i, int radix) :將整數i以基數radix的形式轉換成字串。

例子**:

int i1 = 54321;

system.out.println(「integer.tostring(int i, int radix): 」 + integer.tostring(i1,16) );

結果為:integer.tostring(int i, int radix): d431

23. static integer valueof(string s) :將字串轉換成整數型別。

24. static integer valueof(string s, int radix) :將字串以基數radix的要求轉換成整數型別。

int與integer的區別

1.所佔記憶體不同 integer物件會占用更多的記憶體。integer是乙個物件,需要儲存物件的元資料。但是int是乙個原始型別的資料,所以占用的空間更少。2.型別及初始值 int 是基本型別,直接存數值,在類進行初始化時int類的變數初始為0 而integer是物件 integer是int的封裝...

Integer 和int的區別

1.int是基本的資料型別,直接存數值 2.integer是int的封裝類 integer 是物件,用乙個引用指向這個物件 integer 是乙個類,是int的擴充套件,定義了很多的轉換方法。3.int和integer都可以表示某乙個數值 4.int和integer不能夠互用,因為他們兩種不同的資料...

int和Integer的區別

今天有個學弟問了我乙個面試題 integer a 128 integer b 128 system.out.println a b 輸出什麼?為什麼?說來慚愧,我的直覺告訴我會輸出flase,但突然間還想不出來為什麼。所以寫了個例子研究了下 public static void main strin...