int i =5;//基本类型
Integer n =newInteger(i);//包装类型//将基本类型double变量包装为对象
System.out.println(n);double d =5.6;
Double dx =newDouble(d);
System.out.println(dx);char c ='A';
Character ch =newCharacter(c);
System.out.println(ch);short s =2;
Short sh =newShort(s);
System.out.println(sh);long l =5l;
Long lo =newLong(l);
System.out.println(lo);byte b =3;
Byte by =newByte(b);
System.out.println(by);boolean bo =5>3;
Boolean boo =newBoolean(bo);
System.out.println(boo);float f =5.6f;
Float fl =newFloat(f);
System.out.println(fl);
System.out.println(dx.intValue());
System.out.println(n.doubleValue());
System.out.println(n.floatValue());
Integer ix =newInteger(400);
Byte bx = ix.byteValue();
System.out.println(bx);
Double dl =newDouble(3.141592656946);float fx = dl.floatValue();
System.out.println(fx);//拆包装方法
Integer i2 =newInteger(200);int m = i2.intValue();
System.out.println(m);
2. 自动装箱和自动拆箱
原理为Java的编译器会在编译期间进行代码替换,利用API方法完成装箱和拆箱
好处是可以大大节省代码量,提高开发效率
Integer i =5;//自动装箱int n = i;//自动拆箱
Double dx =5.6;//自动装箱double dn = dx;//自动拆箱
Character c ='A';char ch = c;
Byte b =5;byte bx = b;
Short s =5;short sx = s;
Float f =5.6f;float fx = f;
Long l =5L;long lx = l;
Boolean boo =5>3;boolean bo = boo;//如果包装类型变量值为null时候,进行自动拆包会发生空指针异常//Integer k = null;//int q = k;//k.intValue();
int n =50;
System.out.println(n);
String str ="2683";int m = Integer.parseInt(str);
String s = Integer.toString(m);
System.out.println(m);
str ="3.1415926E10";double d = Double.parseDouble(str);
s = Double.toString(d);
System.out.println(s);