包装类的转化

基本数据类型与包装类的转化:

  1. 基本数据类型转换为包装类称之为自动装箱
  2. 包装类转换为基本数据类型称之为自动拆箱
/*
*通过字符串参数
*/
Float a=new Float("32.1F");
/*
*通过构造器
*/ Integer a=Integer (10);
/*
*自动装箱
*/
int num=10;
Integer it=num;
/*
*通过xxxValue(方法)
*/
Integer a =10;
int a1 = int.valueof(a); /* *自动拆箱 */ Boolean b=true; boolean b1=b;

String 与包装类转换:

String s1="123";
//int num =(int)s1;编译报错
int num=Integer.parseInt(s1);
//String转换为包装类调用包装类Xxx的parseXxx(Strin str)方法
Strin s2="true";
boolean s3=Boolean.parseBoolean(String s2);

 String 与基本数据类型的转化:

//方式1
int num=10;
String s1=10+"";
//方式2
float f1=12.3F;
String s2=String.valueOf(f1);

原文地址:https://www.cnblogs.com/ylblikestudyJava/p/12362295.html