Java 7的6个新特性

Java 8早都出来了,现在来了解一下Java 7语言上的几个新特性。 :) switch语句支持String、数字常量的新形式、改进的异常处理、TWR语句、钻石语法和变参警告位置的修改。

1. switch语句支持String

public void printDay(String dayOfWeek) {
    switch(dayOfWeek) {
        case "Sunday": System.out.println("周日"); break;
        case "Saturday": System.out.println("周六"); break;
        ...
        default: System.out.println("不知道"); break;
    }
}

2. 更强的数值文本表示法

int x = 0b1100110; //0b表示二进制
int bitPattern = 0b0001_1100_0011; //也可以加下划线
long longValue = 2_111_000_888L; //加下划线便于阅读

3. 改善的异常处理

try {
    String fileText = getFile(fileName);
    cfg = verifyConfig(parseConfig(fileText));
} catch(FileNotFoundException | ParseException | ConfigurationException e) {
    //可以用或来表示可能的异常
    ...
} catch(IOException iox) {
    ...
}

另一个新语法对需要重新抛出异常时很有用:

try {
    doSomethingWhichMightThrowIOException();
    doSomethingElseWhichMightThrowSQLException();
} catch (final Exception e) {
    ...
    //不再是抛出笼统的Exception,而是抛出实际的异常。
    //final不是必须的,但留着提个醒有好处。
    throw e;
}

4. TWR(try-with-resources)

这个很有用,特别是io操作时,可以抛掉大串丑陋的代码了。

try (
    OutputStream out = new FileOutputStream(file);
    InputStream is = url.openStream()
) {
    byte[] buf = new byte[4096];
    int len;
    while (len = is.read(buf)) > 0)
        out.write(buf, 0, len);
}

上面的代码将资源放在try的圆括号内,当处理完后会自动关闭!但一定要注意不要嵌套创建,否则可能无法正确关闭。一定要声明变量。例如下面的代码就应该修改:

try (ObjectInputStream in = 
    new ObjectInputStream(
        new FileInputStream("someFile.bin"))) {
    ...
}

//要改为:
try (
    FileInputStream fin = new FileInputStream("someFile.bin");
    ObjectInputStream in = new ObjectInputStream(fin)) {
    ...
}

TWR特性依赖于try从句中的资源类实现新接口AutoCloseable。Java 7平台的大多数资源都已经修改过了。

5. 钻石语法

//不用这么麻烦了:
Map<Integer, Map<String, String>> userList = new HashMap<Integer, Map<String, String>>();

//可以直接写成:
Map<Integer, Map<String, String>> userList = new HashMap<>();

6. 简化变参方法调用

在Java 7之前,如果泛型和变参结合起来会怎么样?

public static <T> Collection<T> doSomething(T... entries) {
    ...
}

Java处理变参实际上是把它放到一个编译器自动创建的数组中。但我们知道泛型的实现其实是通过擦拭法实现的。所以Java数组不支持泛型:

HashMap<String, String>[] a = new HashMap<String, String>[3]; //编译错误

HashMap<String, String>[] a = new HashMap[3]; //编译可通过,但会有警告:
//Type safety: The expression of type HashMap[] needs unchecked conversion to conform to HashMap<String,String>[]

因此,当泛型遇到变参时,编译器只好给你个警告。但这个问题更应该由API的设计者去关注,而不是API使用者。所以Java 7把警告信息挪到了定义API的地方。


转载:http://blog.ubone.com/blog/2014/11/18/java-7de-6ge-xin-te-xing/ 

2014-11-18

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值