为了针对基本类型数据进行更多操作,更方便的操作,Java就针对每一种基本数据类型提供了对应
的类类型。包装类类型。
(基本类型) (引用类型)
byte Byte
short Short
int Interger
long Long
float Float
double Double
char Character
boolean Boolean
/**Interger的构造方法*/
public Integer(int value)
public Interger(String s)
注意:这个字符串必须是有数字字符组成
public static void main(String[] args) {
//方式1
int i = 100;
Integer ii = new Integer(i);
System.out.println("ii:" + ii);
//方式2
String s = "100";
//String s = "abc"; 会报错 NumberFormatException
Integer iii = new Integer(s);
System.out.println("iii:" + iii);
}
/**String和int类型的相互转换*/
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);
/**String -- int*/
String s = "100";
//方式1
//String -- Integer -- int
Integer ii = new Integer(s);
//public int intValue()
int x = ii.intValue();
Systme.out.println("x:" + x);
//方式2
//public static int parseInt(String s)
int y = Integer.parseInt(s);
System.out.println("y" + y);
/**String -- float*/
//public static float parseFloat(String s)
String s = "100";
float f1 = Float.parseFloat(s);
System.out.println(f1);//100.0
}