Q:为什么要有基本数据类型包装类
A:将基本数据类型分装成对象的好处是,可以在对象中定义更多的功能方法操作该数据
Q:常用操作
A:基本数据类型与字符串之间的转换
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
Integer类
package com.heima.wraples; public class Demo2_integer { /* * 构造方法 * public Integer(int value) * public Interger(String a) * 使用构造方法构造对象 * */ public static void main(String[] args) { System.out.println(Integer.MAX_VALUE);//int的最大值2147483647 System.out.println(Integer.MIN_VALUE);//int的最小值-2147483648 Integer i1=new Integer(100); System.out.println(i1); Integer i2=new Integer("100"); //Integer i2=new Integer("abc"); 会出现数字格式异常,因为abc不是数字格式字符串,所以转换会报错 System.out.println(i2); } }
package com.heima.wraples; public class Demo1_Intyerger { public static void main(String[] args) { System.out.println(Integer.toBinaryString(60));//转换成二进制的60 System.out.println(Integer.toHexString(60)); //转换成16进制 } }
类型之间的相互转换
package com.heima.wraples; public class Demo3_Integer { /* *int类转换成String类 * 1.和“”进行拼接 * 2.public static String value(int i) * 3.int--Integer--String(int类的toString方法) * 4.public static String toString(int i)(Integer类的静态方法) * String类转换成int类 * 1.public ststaic int parseInt(String s) * * 基本数据类型包装类有八中,其中七种都有parsexxx的方法,可以将这七种的字符串表现形式转换成xxx。 * char的包装类Character中没有parsexxx的方法 * */ public static void main(String[] args) { demo1();//int转换成String推荐使用前两中转换方法 demo2();//String转换成int } private static void demo2() { //先将String转换成Integer,再调用intValue方法转换成int String s1="523135"; Integer i=new Integer(s1); int a=i.intValue(); System.out.println(a); //用Integer类的静态方法parseInt,开发中推荐 String s2="56456"; int y=Integer.parseInt(s2); System.out.println(s2); } private static void demo1() { int i=100; //字符串拼接 String s1=i+""; System.out.println(s1); //String类的valueof方法 String s2=String.valueOf(20); //先转换成Integer再用里面的Tostringfangfa Integer i2=new Integer(i); String s3=i2.toString(); //直接用Integer类里面的静态方法 String s4=Integer.toString(i); System.out.println(s4); }