1 常用类
包装类 Wrapper
针对八种基本数据类型相应的引用类型 -> 包装类
有了类的特点,就可以调用类中的方法
装箱和拆箱
装箱就是将基础数据类型转换为对应的包装器类型;
拆箱就是将包装器类型转换为对应的基础数据类型;
让基本类型具备对象的特征,实现更多的功能
//boolean原生类型自动装箱成Boolean
public static Boolean valueOf(boolean b) {
return (b ? TRUE : FALSE);
}
//byte原生类型自动装箱成Byte
public static Byte valueOf(byte b) {
final int offset = 128;
return ByteCache.cache[(int)b + offset];
}
//short原生类型自动装箱成Short
public static Short valueOf(short s) {
final int offset = 128;
int sAsInt = s;
if (sAsInt >= -128 && sAsInt <= 127) { // must cache
return ShortCache.cache[sAsInt + offset];
}
return new Short(s);
}
//char原生类型自动装箱成Character
public static Character valueOf(char c) {
if (c <= 127) { // must cache
return CharacterCache.cache[(int)c];
}
return new Character(c);
}
//int原生类型自动装箱成Integer
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
//long原生类型自动装箱成Long
public static Long valueOf(long l) {
final int offset = 128;
if (l >= -128 && l <= 127) { // will cache
return LongCache.cache[(int)l + offset];
}
return new Long(l);
}
//double原生类型自动装箱成Double
public static Double valueOf(double d) {
return new Double(d);
}
//float原生类型自动装箱成Float
public static Float valueOf(float f) {
return new Float(f);
}
包装类 和 String互相转换
Integer -> String
Integer i = 100;
①String str1 = i + " ";
②i.toString.var
③String str3 = String.valueOf(i);
String -> Integer
String str4 = "12345";
①Intege i2 = Integer.parseInt(str4);
②Intege i3 = new Integer(str4);
String
String是final类 不能被其他类继承
String对象用于保存字符串 也就是一组字符序列
字符串常量用双引号括起来
使用unicode编码 一个字符占两个字节
String 实现的接口
Serializable String可以串行化(可以在网络传输)
Comparable String 可以比较
CharSequence String
Object
创建对象
① 直接赋值 String s = " xxx ";
②调用构造器 String s2 = new String("xxx");
StringBuffer
StringBuild
Math
Date Calendar LocalDate
System
Arrays
BigInteger BigDecimal