---
title: "Java7中那些新特性 - 1 (语法语义篇)"
author: "evil850209"
date: "2013-08-09T15:31:40Z"
tags: []
categories: ["编程语言"]
slug: "java7-1"
draft: false
lastmod: "2013-08-09T15:31:40Z"
id: 1922600
url: "https://evil850209.iteye.com/blog/1922600"
---

Java7已经发布了很久了，Java8在明年也即将发布了，但对于Java7中的一些‘新’特性，一直没有系统的去学习过。日常工作中大多数的编程习惯还是基于Java6的，其实有一些重复且繁琐的工作再Java7中有更好的解决方案。最近开始阅读Java 7 New Features Cookbook一书。在此把读书笔记记录下来。 Java7中新增加的特性包括：\[list] \[\*]在switch语句中使用字符串 \[\*]可以在数字变量中使用下划线增强数字的可读性 \[\*]try-with-resources代码块 \[\*]使用catch代码块捕获多个异常 \[\*]使用diamond operator改进泛型引用 \[\*]改进了方法中多可变参数的使用 \[/list] 下面我们逐个举例来了解一下各个新特性。 1 在switch语句中使用字符串可以大大简化以前的一些代码，避免了多个if else的出现。其实有趣的是，Java虚拟机并没有提供switch语句对string的支持，是Java编译器将String转换成了相应的byte code。 使用string做switch语句条件时要注意两点： \[list] \[\*]Null对string的影响 \[\*]大小写的区分 \[/list] \[code="java"] public class StringSwitchExample { private static boolean verbose = false; private static boolean logging = false; private static boolean displayHelp = false; public static void main(String\[] args) { for (String argument : args) { switch (argument) { case "-verbose": case "-v": verbose = true; break; case "-log": logging = true; break; case "-help": displayHelp = true; break; default: } } System.out.println("Illegal command line argument"); displayApplicationSettings(); } private static void displayApplicationSettings() { System.out.println("Application Settings"); System.out.println("Verbose: " + verbose); System.out.println("Logging: " + logging); System.out.println("Help: " + displayHelp); } } \[/code] 2 在Java7中数字变量的定义可以使用下划线(\_)来增强可读性。对于那些与货币有关的系统，还是建议使用\[b]java.util.Currency\[/b]更为合适。 \[code="java"] public class UnderscoreSimple { public static void main(String\[] args) { long debitCard = 1234\_5678\_9876\_5432L; System.out.println("The card number is: " + debitCard); System.out.print("The formatted card number is:"); printFormatted(debitCard); float minAmount = 5\_000F; float currentAmount = 5\_250F; float withdrawalAmount = 500F; if ((currentAmount - withdrawalAmount) &lt; minAmount) { System.out.println("Minimum amount limit exceeded " + minAmount); } } private static void printFormatted(long cardNumber) { String formattedNumber = Long.toString(cardNumber); for (int i = 0; i &lt; formattedNumber.length(); i++) { if (i % 4 == 0) { System.out.print(" "); } System.out.print(formattedNumber.charAt(i)); } System.out.println(); } } \[/code] 3 Java7之前，我们需要对可关闭的资源做恰当的处理，通常是要在finally代码块中关闭需要被关闭的资源（java.io.InputStream或java.nio.Channel）。但在Java7中，如果一个类实现了java.lang.AutoCloseable接口，我们就可以通过try-with-resources代码块来自动关闭这些资源。 \[code="java"] public class AutoCloseableExample { public static void main(String\[] args) { String sourcePath = "file:///D:/home/docs/users.txt"; String destPath = "file:///D:/home/docs/users.bak"; Charset charset = Charset.defaultCharset(); try (BufferedReader inputReader = Files.newBufferedReader(Paths.get(new URI(sourcePath)), charset); BufferedWriter outputWriter = Files.newBufferedWriter(Paths.get(new URI(destPath)), charset)) { String inputLine; while ((inputLine = inputReader.readLine()) != null) { outputWriter.write(inputLine); outputWriter.newLine(); } System.out.println("Copy complete!"); } catch (URISyntaxException | IOException ex) { ex.printStackTrace(); } } } \[/code] 4 使用Catching multiple exception types来避免冗长的异常捕捉。在Java7中我们可以用一个catch语句来捕获代码块中抛出的多个异常。 \[code="java"]public class InvalidParameter extends java.lang.Exception { public InvalidParameter() { super("Invalid Parameter"); } }\[/code] \[code="java"]public class MultipleExceptions { private static final Logger logger = Logger.getLogger("log.txt"); public static void main(String\[] args) { System.out.print("Enter a number: "); try (Scanner scanner = new Scanner(System.in)) { int number = scanner.nextInt(); if (number &lt; 0) { throw new InvalidParameter(); } System.out.println("The number is: " + number); } catch (InputMismatchException | InvalidParameter e) { logger.log(Level.INFO, "Invalid input, try again"); } } }\[/code] 5 使用diamond operator简化了对泛型对象的创建。 \[code="java"]List list = new ArrayList&lt;&gt;(); List&gt; stringList = new ArrayList&lt;&gt;();\[/code] 6 @SafeVarargs和@SuppressWarnings这两种注解是用来处理那些通常无害的警告信息的。顾名思义@SuppressWarnings是用来消除那些特殊类型的警告的。Java7中引进的@SafeVarargs注解是用来指定一些方法或构造方法是安全的，这些方法或构造方法使用了不定长参数。如果传递的不定长参数是泛型的，我们可以给方法加上@SafeVarargs来消除无害的警告。 \[code="java"]public class SafeVargExample { @SafeVarargs public static void displayElements(T... array) { for (T element : array) { System.out.println(element.getClass().getName() + ": " + element); } } public static void main(String\[] args) { ArrayList a1 = new ArrayList&lt;&gt;(); a1.add(new Integer(1)); a1.add(2); ArrayList a2 = new ArrayList&lt;&gt;(); a2.add(new Float(3.0)); a2.add(new Float(4.0)); displayElements(a1, a2, 12); } }\[/code]