0
点赞
收藏
分享

微信扫一扫

Java 中 基本类型、包装类与String类间的转换

有态度的萌狮子 2022-04-13 阅读 48
javaeclipse

基本数据类型 包装类

——————————————————————————————

基本数据类型 包装类

  1. 通过构造器:Integer t = new Integer( );
//方式一:
int num1 = 22;
Integer num2 = new Integer(num1);
System.out.prinln(num2.toString());
  1. 通过字符串参数:Float f = new Float (" ");
Integer num2 = new Integer("12");
System.out.println(num2.toString());
  1. 自动装箱(JDK 5.0 新特性):自动转换

包装类 基本数据类型

  1. 调用包装类的方法: xxxValue( )
Integer num1 = new Integer(22);

int num2 = num1.intValue();
System.out.println(num2);
  1. 自动拆箱(JDK 5.0 新特性):自动转换

——————————————————————————————


基本数据类型、包装类 String类

——————————————————————————————
 

基本数据类型、包装类 String类

  1. String类的valueOf(3.4f) 方法
float f1 = 12.1f;
String str1 = String.valueOf(f1);
  1. 22.3 + “”
int num1 = 10;
String str1 = num1 + "";

String类 基本数据类型、包装类

  1. 调用相应的包装类的parseXxx(String ) 静态方法。
String str1 = "123";
int num1 = Integer.parseInt(str1);
System.out.println(num1);
  1. 通过包装类构造器:boolean b = new Boolean(“true”);

——————————————————————————————

举报

相关推荐

0 条评论