Java基本类型包装类概述与Integer类、Character类用法分析

2025-05-29 0 69

本文实例讲述了java基本类型包装类概述与integer类、character类用法。分享给大家供大家参考,具体如下:

基本类型包装类概述

将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据。

常用的操作之一:用于基本数据类型与字符串之间的转换。

基本类型和包装类的对应

byte,short,integer,long,float,doublecharacter,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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java基本类型包装类概述与Integer类、Character类用法分析 https://www.kuaiidc.com/109289.html

相关文章

发表评论
暂无评论