进阶day01-字符串与基本类型的转换
This commit is contained in:
55
javaSE-day01/src/com/inmind/wrap08/Demo26.java
Normal file
55
javaSE-day01/src/com/inmind/wrap08/Demo26.java
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
package com.inmind.wrap08;
|
||||||
|
/*
|
||||||
|
在java中定义了8个与基本类型一一对应的包装类
|
||||||
|
byte Byte
|
||||||
|
short Short
|
||||||
|
int Integer
|
||||||
|
long Long
|
||||||
|
float Float
|
||||||
|
double Double
|
||||||
|
char Character
|
||||||
|
boolean Boolean
|
||||||
|
|
||||||
|
包装类是一个类,封装了很多的功能,提供更多的功能去操作,基本类型进行计算
|
||||||
|
|
||||||
|
在jdk1.5版本之后,出现自动拆装箱
|
||||||
|
拆箱:包装类--->基本类型
|
||||||
|
装箱:基本类型--->包装类
|
||||||
|
------------------------------------------------
|
||||||
|
字符串与基本类型的转换
|
||||||
|
|
||||||
|
基本类型---->字符串
|
||||||
|
+""即可
|
||||||
|
字符串----->基本类型
|
||||||
|
包装类名.parseXXX(字符串);
|
||||||
|
*/
|
||||||
|
public class Demo26 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int i = 10;
|
||||||
|
Integer i1 = i;//装箱
|
||||||
|
Integer i2 = new Integer(10);
|
||||||
|
System.out.println(i2);
|
||||||
|
|
||||||
|
// int sum = 10+i2;//拆箱
|
||||||
|
int sum = 10 + i2.intValue();
|
||||||
|
System.out.println(sum);
|
||||||
|
System.out.println("--------------------------------------");
|
||||||
|
//基本类型转为字符串
|
||||||
|
int a = 10;
|
||||||
|
String str1 = a + "";
|
||||||
|
|
||||||
|
//字符串转为对应的基本类型
|
||||||
|
String str2 = "true";
|
||||||
|
boolean b1 = Boolean.parseBoolean(str2);
|
||||||
|
System.out.println(b1);
|
||||||
|
|
||||||
|
String str3 = "100";
|
||||||
|
int i3 = Integer.parseInt(str3);
|
||||||
|
System.out.println(i3);
|
||||||
|
|
||||||
|
String str4 = "2.0";
|
||||||
|
double v = Double.parseDouble(str4);
|
||||||
|
System.out.println(v);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user