jdk 是整个Java的核心,包括了Java运行环境(Java Runtime Envirnment),一堆Java工具和Java基础的类库(rt.jar), SUN公司不断对它进行升级,发布了不同的版本,jdk1.5 是Sun公司后来发行的版本也就是jdk5.0(JDK1.5 更名后)
2004年9月30日18:00PM,J2SE1.5发布,是Java语言的发展史上的又一里程碑事件。为了表示这个版本的重要性,J2SE1.5更名为J2SE5.0。
JDK1.5新特性
自动装箱与拆箱
Integer iObj = 3; System.out.println(iObj + 12); Integer i1 = 137(-128--127范围时,为true); Integer i2 = 137(-128--127范围时,为true); System.out.println(i1 == i2); //false,但是括号中时却返回ture,原因是Integer采用的是享元模式 Integer i3 = Integer.valueOf(213); Integer i4 = Integer.valueOf(213); System.out.println(i3==i4);//同上,另一种包装形式
枚举(常用来设计单例模式)
public class EnumTest { public static void main(String[] args) { WeekDay1 weekDay = WeekDay1.MON; System.out.println(weekDay.nextDay()); WeekDay weekDay2 = WeekDay.FRI; System.out.println(weekDay2); System.out.println(weekDay2.name()); System.out.println(weekDay2.ordinal()); System.out.println(WeekDay.valueOf("SUN").toString()); System.out.println(WeekDay.values().length); new Date(300){}; } public enum WeekDay{ SUN(1),MON(),TUE,WED,THI,FRI,SAT; private WeekDay(){System.out.println("first");} private WeekDay(int day){System.out.println("second");} } public enum TrafficLamp{ RED(30){ public TrafficLamp nextLamp(){ return GREEN; } }, GREEN(45){ public TrafficLamp nextLamp(){ return YELLOW; } }, YELLOW(5){ public TrafficLamp nextLamp(){ return RED; } }; public abstract TrafficLamp nextLamp(); private int time; private TrafficLamp(int time){this.time = time;} } }
静态导入
import static java.lang.Math.*; public class StaticImport { public static void main(String[] args){ int x = 1; try { x++; } finally { System.out.println("template"); } System.out.println(x); System.out.println(max(3, 6)); System.out.println(abs(3 - 6)); } }
可变参数
public class VarableParameter { public static void main(String[] args) { System.out.println(add(2,3)); System.out.println(add(2,3,5)); } public static int add(int x,int... args){ int sum = x; for(int arg : args){ sum += arg; } return sum; } }
内省
ReflectPoint pt1 = new ReflectPoint(3,5); BeanInfo beanInfo = Introspector.getBeanInfo(pt1.getClass()); PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors(); Object retVal = null; for(PropertyDescriptor pd : pds){ Method methodGetX = pd.getReadMethod(); retVal = methodGetX.invoke(pt1); }
: » Java jdk1.5 新特性(jdk1.5到jdk1.8的新特性系列)
原创文章,作者:wdmbts,如若转载,请注明出处:https://blog.ytso.com/251492.html