常见类
包装类
主动装箱(int->Integer): Integer.valueOf(i)
主动拆箱(Integer->int): integer.intValue()
当包装类类和根本类型比拟的时候,会将包装类主动拆箱,所以比拟后果总是True。
Integer中有一个缓存,在主动装箱的时候Integer i = 1
,调用valueOf办法将根本数据类型转化为Integer对象,如果这个数字i >= IntegerCache.low && i <= IntegerCache.high
个别是-128~127,则会从缓存的cache数组中取出对象而不创立新的对象,否则调用new Integer()
// Integer源码public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i);}
如果新建了Integer对象,那么雷同数值对应的Integer对象也会不同。即i1 == i2
返回False,否则返回True,因为都是取了cache中的对象,没有新建,提高效率(不设置更大范畴,优化内存,只设置罕用的)。
Double中就没有cache对象数组。
String
String str = "abc"
字符串放在常量池,在1.7以前在办法区,1.7之后在堆空间。和String str = new String("abc")
的地址不同,后者是指向对象。
String为finally对象,不能被继承。
String为不可变对象,指的是String类内的private final char[] value
数组是惟一对应的,value不能扭转援用的数组,然而value利用数组的值能够通过反射进行拜访和批改。
equals()
对象地址雷同返回True,否则取每个字符串的value赋值给字符串数组,而后逐位字符比拟。
charAt(i)
间接返回value[i]
intern()
如果常量池外面蕴含str中的字符串值(用equals判断),则间接返回这个常量地址。
String a = "abc";String b = new String("abc");print(a == b); // False 一个指向常量池,一个指向对象print(a == b.intern()); // True intern()找到常量池中abc的地址
StringBuffer & StringBuilder
StringBuilder: 效率高
StringBuffer: 线程平安(synchronized)
日期格式化
Date date = new Date();// pattern中,yyyy代表年保留四位数字,其余同理,如果让月日等信息保留两位数的格局就要写两个// 字母代表的意思能够到官网查apiDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println(dataFormat.format(date));
在定义好dataFormat对象当前,它曾经晓得你的日期格局,就能够应用date = dataFormat.parse("2020-1-1 10:19:21");
来失去date对象,与format作用相同。
Calendar用法:
// 用dateFormat结构出date对象DateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = dataFormat.parse("2020-1-1 10:19:21");// Calendar用法:不能间接new,因为其构造方法为protected,要应用getInstanceCalendar calendar = Calendar.getInstance();calendar.setTime(date); // 将date对象传入,否则展现的是以后工夫// get()的参数为int,在Calendar中有对应各个值对应的常量序号System.out.println(calendar.get(Calendar.YEAR));System.out.println(calendar.get(Calendar.MONTH));System.out.println(calendar.get(Calendar.DATE));
枚举类
须要应用一组常量时,倡议应用枚举类。
用法示例
public class EnumTest{ public static void main(String[] args) { System.out.println("间接用常量名拜访:" + Size.SMALL); Scanner in = new Scanner(System.in); System.out.print("Enter a size: (SMALL, MEDIUM, LARGE, EXTRA_LARGE) "); String input = in.next().toUpperCase(); // 疏忽大小写(全副转大写) Size size = Enum.valueOf(Size.class, input); // 依据输出的值,通过反射获取枚举类型中的常量 System.out.println("size=" + size); System.out.println("abbreviation=" + size.getAbbreviation()); if (size == Size.EXTRA_LARGE) // 比拟时,用 ==,而不须要用equals System.out.println("Good job--you paid attention to the _."); }}enum Size{ SMALL("S"), MEDIUM("M"), LARGE("L"), EXTRA_LARGE("XL"); private Size(String abbreviation) { this.abbreviation = abbreviation; } // 只能由编译器调用 public String getAbbreviation() { return abbreviation; } private String abbreviation;}
Enter a size: (SMALL, MEDIUM, LARGE, EXTRA_LARGE) SMALL间接用常量名拜访:SMALLsize=SMALLabbreviation=S
如果写了abbreviation参数,如SMALL("S")这种模式,就肯定要定义构造方法private Size(String abbreviation),否则会报错。