JAVA中基本数据类型转换包装类,以及基本数据类型转换字符串的知识点总结

首先说基本数据类型总共以下8种
整数型 byte short int long
浮点型 float double
字符型 char
布尔型 boolean

包装类的由来:虽然基本数据类型使用起来很方便,但是没有对应的方法来操作这些基本类型的数据,因此我们可以使用一个类把基本类型的数据都封装起来,在类中定义一些方法,这个类就是我们的包装类。

而其对应关系为
byte --> Byte
short --> Short
int --> Integer
long --> Long
float --> Float
double --> Double
char --> Character
boolean -> Boolean

而我们所说的装箱就是把基本数据类型转换为包装类
而我们所说的拆箱就是把包装类转换为基本数据类型
以Integer类型举例
1装箱
1.1构造方法
1.1.1 采用Integer(int value)的构造方法,将int类型转换为Integer
Integer integer01=new Integer(1);

1.1.2 采用Integer(String s)的构造方法,将String类型转换为Integer。
注意这里的字符串必须是基本类型的字符串。例如“123”,否则就会抛出异常,例如“ab2”
Integer integer02=new Integer(“123”);

1.2 静态方法
1.2.1 static Integer valueOf(int i),返回指定的int值的Integer实例
Integer integer03=Integer.valueOf(1);
1.2.2 static Integer valueOf(String s),返回指定的String值的Integer实例
Integer integer04=Integer.valueOf(“123”);

1.3 自动装箱
int i01=1
Integer i05=i01;

2拆箱

2.1 成员方法
int intValue()以int类型返回该Integer值
int i02=integer05.intValue();

2.2 自动拆箱
int i03=integer05;

——————————华丽的分割线———————————
基本数据类型与字符串类型的相互转换
1基本数据类型 -->字符串
1.1 基本数据类型+"";//空字符串
int i04=5;
String string01=i04+"";

扫描二维码关注公众号,回复: 9197056 查看本文章

1.2 对应包装类的静态方法toString(参数)
String string02=Integer.toString(100);

1.3 String类的静态方法 valueOf(参数)
static String valueOf(int i):返回int类型的字符串表现形式
String string03=String.valurOf(100);

2 字符串 -->基本数据类型

2.1 使用对应包装类的静态方法
static int paseInt(String s):
int i05=Integer.parseInt(string03);
System.out.println(i05-10);//90

发布了8 篇原创文章 · 获赞 0 · 访问量 265

猜你喜欢

转载自blog.csdn.net/codeLearner_CXW/article/details/104343188
今日推荐