1.基本数据类型对象包装类:
为了方便操作基本数据类型值,将其封装成了对象,在对象中定义了属性和行为,丰富了该数据的操作。
byte : Byte
short : Short
int : Integer
long : Long
float: Float
double: Double
char: Character
boolean: Boolean
2.和String类型相互转换:
基本类型 转换 String:
1.基本数据类型数值 + ""
2.用String类中的静态方法valuOf(基本类型数值)
String 转换 基本类型:
1.使用包装类中的静态方法 xxx parseXxx("xxx类型的字符串"):
int parseInt("intstring");
long parseLong("longstring");
boolean parseBoolean("true/false");
...
只用Character没有parse方法,不需要。
2.如果字符串被Integer进行了封装。
可以用另一个非静态的方法,intValue():
Integer i = new Integer("123");
i.intValue();
3.进制转换:
1.十进制 转换 其他进制:
String toBinaryString(int i); 转换成二进制
toOctalString(int i); 转换成八进制
toHexString(int i); 转换成十六进制
toString(int i, int radix); 转换成指定进制
2.其他进制 转换 十进制:
int parseInt(String s, int radix);
(Integer valueOf(String s, int radix); 返回的值不能运算,还是字符串。)
4:数字对象的运算:
//数字对象的运算:
Integer a = new Integer("3");
Integer b = new Integer(3);
Integer c = new Integer(1);
System.out.println(a==b); //false
System.out.println(a.equals(b)); //true , 比较数值是否相同
System.out.println(c.compareTo(a)); //比较
5.JDK1.5自动装箱拆箱:
int num = 4;
num = num + 5;
Integer i =4; //等效,Integer i = new Integer(4); 自动装箱,就是简化书写。
//自动装箱和不同赋值相比,多了一个空null。
//所以要先判断是否为null。因为空类型在拆箱的时候会报空指针异常。
i = i + 6; //等效i = new Integer(i.intValue() + 6); 自动装箱,拆箱(i.intValue())。
show(55);
public static void show(Object a){ //Object a = new Ingeter(55); 装箱,多态
}
面试:自定装箱,如果装箱的是一个字节,那么该数据会被共享,不会重新开辟空间。
Integer a = new Integer(127);
Integer b = new Integer(127);
System.out.println(a==b); //false
System.out.println(a.equals(b)); //true
Integer x = 127;
Integer y = 127;
System.out.println(x==y); //true, 自定装箱,如果装箱的是一个字节,那么该数据会被共享,不会重新开辟空间。
System.out.println(x.equals(y)); //true