Java ·

一起看看JDK5带给我们哪些新特性

JDK1.5新特性:
1. 自动装箱与拆箱(Autoboxing/unboxing)

Integer iObj = 3;
System.out.println(iObj + 12);

Integer i1 = 137(-128--127范围时,为true);
Integer i2 = 137(-128--127范围时,为true);
//false,但是括号中时却返回ture,原因是Integer采用的是享元模式
System.out.println(i1 == i2);

Integer i3 = Integer.valueOf(213);
Integer i4 = Integer.valueOf(213);
//同上,另一种包装形式
System.out.println(i3==i4);

2. 枚举(常用来设计单例模式)

package tojsp.com;

import java.util.Date;

public class EnumTest {
    /**
     * @param args
     */
    public static void main(String[] args) {
        WeekDay weekDay = WeekDay.MON;
        System.out.println(weekDay.name());
        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;
        }
    }
}

3. 静态导入

package tojsp.com;

import static java.lang.Math.abs;
import static java.lang.Math.max;

public class EnumTest {
    public static void main(String[] args) {
        int x = 1;
        try {
            x++;
        } finally {
            System.out.println("www.tojsp.com");
        }

        System.out.println(x);

        System.out.println(max(3, 6));
        System.out.println(abs(3 - 6));

    }
}

4. 可变参数

package tojsp.com;

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;
    }
}

5. 内省

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);

}

6. 泛型
泛型是JDK1.5中一个最“酷”的特征。通过引入泛型,我们将获得编译时类型的安全和运行时更小地抛出ClassCastExceptions的可能。在JDK1.5中,你可以声明一个集合将接收/返回的对象的类型。

在JDK1.4中,创建雇员名字的清单(List)需要一个集合对象:
List listOfEmployeeName = new ArrayList();

在JDK1.5中,你将使用下面语句:

List<String> listOfEmployeeName = new ArrayList<String>();

最“酷”的是,如果你试图插入非string类型的值,你将在编译时发现并且修正这类问题。没有泛型,你会发现这样一个bug,当你的客户调用后会告诉你,你所编写的程序抛出ClassCastException异常而崩溃。

另外,当你从集合中得到一个元素时你无需进行强制转换。故原先为:

String employeeName = ((String) listOfEmployee.get(i));

而下面的语句将比上面的更加简单:

String employeeName = listOfEmployee.get(i);

参与评论

  • burberry online store

    Very good post, thank you a lot for sharing. Do you might have an RSS feed I can subscribe to?

    6年前 (2014-03-24)
    回复
  • Leo

    tks, RSS feed at the right top of website, you can got it.

    6年前 (2014-03-25)
    回复
    回复Leo