本文實例講述了java基本類型包裝類概述與integer類、character類用法。分享給大家供大家參考,具體如下:
基本類型包裝類概述
將基本數據類型封裝成對象的好處在于可以在對象中定義更多的功能方法操作該數據。
常用的操作之一:用于基本數據類型與字符串之間的轉換。
基本類型和包裝類的對應
byte,short,integer,long,float,double,character,boolean
integer類
為了讓基本類型的數據進行更多的操作,java就為每種基本類型提供了對應的包裝類類型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
package cn.itcast_01; /* * 需求1:我要求大家把100這個數據的二進制,八進制,十六進制計算出來 * 需求2:我要求大家判斷一個數據是否是int范圍內的。 * 首先你的知道int的范圍是多大? * * 為了對基本數據類型進行更多的操作,更方便的操作,java就針對每一種基本數據類型提供了對應的類類型。包裝類類型。 * byte byte * short short * int integer * long long * float float * double double * char character * boolean boolean * * 用于基本數據類型與字符串之間的轉換。 */ public class integerdemo { public static void main(string[] args) { // 不麻煩的就來了 // public static string tobinarystring(int i) system.out.println(integer.tobinarystring( 100 )); // public static string tooctalstring(int i) system.out.println(integer.tooctalstring( 100 )); // public static string tohexstring(int i) system.out.println(integer.tohexstring( 100 )); // public static final int max_value system.out.println(integer.max_value); // public static final int min_value system.out.println(integer.min_value); } } |
integer的構造方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package cn.itcast_02; /* * integer的構造方法: * public integer(int value) * public integer(string s) * 注意:這個字符串必須是由數字字符組成 */ public class integerdemo { public static void main(string[] args) { // 方式1 int i = 100 ; integer ii = new integer(i); system.out.println( "ii:" + ii); // 方式2 string s = "100" ; // numberformatexception // string s = "abc";//這個字符串必須是由數字字符組成 integer iii = new integer(s); system.out.println( "iii:" + iii); } } |
string和int的相互轉換
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
package cn.itcast_03; /* * int類型和string類型的相互轉換 * * int -- string * string.valueof(number) * * string -- int * integer.parseint(s) */ public class integerdemo { public static void main(string[] args) { // int -- string int number = 100 ; // 方式1 string s1 = "" + number; system.out.println( "s1:" + s1); // 方式2 string s2 = string.valueof(number); system.out.println( "s2:" + s2); // 方式3 // int -- integer -- string integer i = new integer(number); string s3 = i.tostring(); system.out.println( "s3:" + s3); // 方式4 // public static string tostring(int i) string s4 = integer.tostring(number); system.out.println( "s4:" + s4); system.out.println( "-----------------" ); // string -- int string s = "100" ; // 方式1 // string -- integer -- int integer ii = new integer(s); // public int intvalue() int x = ii.intvalue(); system.out.println( "x:" + x); //方式2 //public static int parseint(string s) int y = integer.parseint(s); system.out.println( "y:" +y); } } |
integer的進制轉換的操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
package cn.itcast_04; /* * 常用的基本進制轉換 * public static string tobinarystring(int i) * public static string tooctalstring(int i) * public static string tohexstring(int i) * * 十進制到其他進制 * public static string tostring(int i,int radix) * 由這個我們也看到了進制的范圍:2-36 * 為什么呢?0,...9,a...z,加起來36個 * * 其他進制到十進制 * public static int parseint(string s,int radix) */ public class integerdemo { public static void main(string[] args) { // 十進制到二進制,八進制,十六進制 system.out.println(integer.tobinarystring( 100 )); system.out.println(integer.tooctalstring( 100 )); system.out.println(integer.tohexstring( 100 )); system.out.println( "-------------------------" ); // 十進制到其他進制 system.out.println(integer.tostring( 100 , 10 )); system.out.println(integer.tostring( 100 , 2 )); system.out.println(integer.tostring( 100 , 8 )); system.out.println(integer.tostring( 100 , 16 )); system.out.println(integer.tostring( 100 , 5 )); system.out.println(integer.tostring( 100 , 7 )); system.out.println(integer.tostring( 100 , - 7 )); system.out.println(integer.tostring( 100 , 70 )); system.out.println(integer.tostring( 100 , 1 )); system.out.println(integer.tostring( 100 , 17 )); system.out.println(integer.tostring( 100 , 32 )); system.out.println(integer.tostring( 100 , 37 )); system.out.println(integer.tostring( 100 , 36 )); system.out.println( "-------------------------" ); //其他進制到十進制 system.out.println(integer.parseint( "100" , 10 )); system.out.println(integer.parseint( "100" , 2 )); system.out.println(integer.parseint( "100" , 8 )); system.out.println(integer.parseint( "100" , 16 )); system.out.println(integer.parseint( "100" , 23 )); //numberformatexception //system.out.println(integer.parseint("123", 2)); } } |
jdk5的新特性--自動裝箱和自動拆箱
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
package cn.itcast_05; /* * jdk5的新特性 * 自動裝箱:把基本類型轉換為包裝類類型 * 自動拆箱:把包裝類類型轉換為基本類型 * * 注意一個小問題: * 在使用時,integer x = null;代碼就會出現nullpointerexception。 * 建議先判斷是否為null,然后再使用。 */ public class integerdemo { public static void main(string[] args) { // 定義了一個int類型的包裝類類型變量i // integer i = new integer(100); integer ii = 100 ; ii += 200 ; system.out.println( "ii:" + ii); // 通過反編譯后的代碼 // integer ii = integer.valueof(100); //自動裝箱 // ii = integer.valueof(ii.intvalue() + 200); //自動拆箱,再自動裝箱 // system.out.println((new stringbuilder("ii:")).append(ii).tostring()); integer iii = null ; // nullpointerexception,如果iii為空對象,會報錯,需要判斷是否為空 if (iii != null ) { iii += 1000 ; system.out.println(iii); } } } |
-128到127之間的數據緩沖池問題
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
package cn.itcast_06; /* * 看程序寫結果 * * 注意:integer的數據直接賦值,如果在-128到127之間,會直接從緩沖池里獲取數據 */ public class integerdemo { public static void main(string[] args) { integer i1 = new integer( 127 ); integer i2 = new integer( 127 ); system.out.println(i1 == i2); system.out.println(i1.equals(i2)); system.out.println( "-----------" ); integer i3 = new integer( 128 ); integer i4 = new integer( 128 ); system.out.println(i3 == i4); system.out.println(i3.equals(i4)); system.out.println( "-----------" ); integer i5 = 128 ; integer i6 = 128 ; system.out.println(i5 == i6); system.out.println(i5.equals(i6)); system.out.println( "-----------" ); integer i7 = 127 ; integer i8 = 127 ; system.out.println(i7 == i8); //true system.out.println(i7.equals(i8)); // 通過查看源碼,我們就知道了,針對-128到127之間的數據,做了一個數據緩沖池,如果數據是該范圍內的,每次并不創建新的空間 // integer ii = integer.valueof(127); } } |
character
character 類在對象中包裝一個基本類型 char 的值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package cn.itcast_01; /* * character 類在對象中包裝一個基本類型 char 的值 * 此外,該類提供了幾種方法,以確定字符的類別(小寫字母,數字,等等),并將字符從大寫轉換成小寫,反之亦然 * * 構造方法: * character(char value) */ public class characterdemo { public static void main(string[] args) { // 創建對象 // character ch = new character((char) 97); character ch = new character( 'a' ); system.out.println( "ch:" + ch); } } |
character 類,常見方法。
確定字符的類別(小寫字母,數字,等等),并將字符從大寫轉換成小寫
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
package cn.itcast_02; /* * public static boolean isuppercase(char ch):判斷給定的字符是否是大寫字符 * public static boolean islowercase(char ch):判斷給定的字符是否是小寫字符 * public static boolean isdigit(char ch):判斷給定的字符是否是數字字符 * public static char touppercase(char ch):把給定的字符轉換為大寫字符 * public static char tolowercase(char ch):把給定的字符轉換為小寫字符 */ public class characterdemo { public static void main(string[] args) { // public static boolean isuppercase(char ch):判斷給定的字符是否是大寫字符 system.out.println( "isuppercase:" + character.isuppercase( 'a' )); system.out.println( "isuppercase:" + character.isuppercase( 'a' )); system.out.println( "isuppercase:" + character.isuppercase( '0' )); system.out.println( "-----------------------------------------" ); // public static boolean islowercase(char ch):判斷給定的字符是否是小寫字符 system.out.println( "islowercase:" + character.islowercase( 'a' )); system.out.println( "islowercase:" + character.islowercase( 'a' )); system.out.println( "islowercase:" + character.islowercase( '0' )); system.out.println( "-----------------------------------------" ); // public static boolean isdigit(char ch):判斷給定的字符是否是數字字符 system.out.println( "isdigit:" + character.isdigit( 'a' )); system.out.println( "isdigit:" + character.isdigit( 'a' )); system.out.println( "isdigit:" + character.isdigit( '0' )); system.out.println( "-----------------------------------------" ); // public static char touppercase(char ch):把給定的字符轉換為大寫字符 system.out.println( "touppercase:" + character.touppercase( 'a' )); system.out.println( "touppercase:" + character.touppercase( 'a' )); system.out.println( "-----------------------------------------" ); // public static char tolowercase(char ch):把給定的字符轉換為小寫字符 system.out.println( "tolowercase:" + character.tolowercase( 'a' )); system.out.println( "tolowercase:" + character.tolowercase( 'a' )); } } |
統計一個字符串中大寫字母字符,小寫字母字符,數字字符出現的次數
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
package cn.itcast_03; import java.util.scanner; /* * 統計一個字符串中大寫字母字符,小寫字母字符,數字字符出現的次數。(不考慮其他字符) * * 分析: * a:定義三個統計變量。 * int bigcont=0; * int smalcount=0; * int numbercount=0; * b:鍵盤錄入一個字符串。 * c:把字符串轉換為字符數組。 * d:遍歷字符數組獲取到每一個字符 * e:判斷該字符是 * 大寫 bigcount++; * 小寫 smalcount++; * 數字 numbercount++; * f:輸出結果即可 */ public class charactertest { public static void main(string[] args) { // 定義三個統計變量。 int bigcount = 0 ; int smallcount = 0 ; int numbercount = 0 ; // 鍵盤錄入一個字符串。 scanner sc = new scanner(system.in); system.out.println( "請輸入一個字符串:" ); string line = sc.nextline(); // 把字符串轉換為字符數組。 char [] chs = line.tochararray(); // 歷字符數組獲取到每一個字符 for ( int x = 0 ; x < chs.length; x++) { char ch = chs[x]; // 判斷該字符 if (character.isuppercase(ch)) { bigcount++; } else if (character.islowercase(ch)) { smallcount++; } else if (character.isdigit(ch)) { numbercount++; } } // 輸出結果即可 system.out.println( "大寫字母:" + bigcount + "個" ); system.out.println( "小寫字母:" + smallcount + "個" ); system.out.println( "數字字符:" + numbercount + "個" ); } } |
ps:這里再為大家推薦幾款功能相似的在線工具供大家參考:
在線任意進制轉換工具:https://tool.zzvips.com/t/hex/
字數統計工具:https://tool.zzvips.com/t/textcount/
在線字母大小寫轉換工具:https://tool.zzvips.com/t/daxiaoxie/
希望本文所述對大家java程序設計有所幫助。
原文鏈接:https://www.cnblogs.com/baiyangyuanzi/p/6861718.html