读书人

Java7中那些新特性 - 一 (语法语义篇)

发布时间: 2013-08-13 16:43:28 作者: rapoo

Java7中那些新特性 - 1 (语法语义篇)
Java7已经发布了很久了,Java8在明年也即将发布了,但对于Java7中的一些‘新’特性,一直没有系统的去学习过。日常工作中大多数的编程习惯还是基于Java6的,其实有一些重复且繁琐的工作再Java7中有更好的解决方案。最近开始阅读Java 7 New Features Cookbook一书。在此把读书笔记记录下来。

Java7中新增加的特性包括:在switch语句中使用字符串可以在数字变量中使用下划线增强数字的可读性try-with-resources代码块使用catch代码块捕获多个异常使用diamond operator改进泛型引用改进了方法中多可变参数的使用

下面我们逐个举例来了解一下各个新特性。

1 在switch语句中使用字符串可以大大简化以前的一些代码,避免了多个if else的出现。其实有趣的是,Java虚拟机并没有提供switch语句对string的支持,是Java编译器将String转换成了相应的byte code。

使用string做switch语句条件时要注意两点:
Null对string的影响大小写的区分

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


2 在Java7中数字变量的定义可以使用下划线(_)来增强可读性。对于那些与货币有关的系统,还是建议使用java.util.Currency更为合适。

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) < 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 < formattedNumber.length(); i++) {            if (i % 4 == 0) {                System.out.print(" ");            }            System.out.print(formattedNumber.charAt(i));        }        System.out.println();    }}


3 Java7之前,我们需要对可关闭的资源做恰当的处理,通常是要在finally代码块中关闭需要被关闭的资源(java.io.InputStream或java.nio.Channel)。但在Java7中,如果一个类实现了java.lang.AutoCloseable接口,我们就可以通过try-with-resources代码块来自动关闭这些资源。

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


4 使用Catching multiple exception types来避免冗长的异常捕捉。在Java7中我们可以用一个catch语句来捕获代码块中抛出的多个异常。

public class InvalidParameter extends java.lang.Exception {    public InvalidParameter() {        super("Invalid Parameter");    }}


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 < 0) {                throw new InvalidParameter();            }            System.out.println("The number is: " + number);        } catch (InputMismatchException | InvalidParameter e) {            logger.log(Level.INFO, "Invalid input, try again");        }    }}


5 使用diamond operator简化了对泛型对象的创建。

List<String> list = new ArrayList<>();List<Map<String, List<String>> stringList = new ArrayList<>();


6 @SafeVarargs和@SuppressWarnings这两种注解是用来处理那些通常无害的警告信息的。顾名思义@SuppressWarnings是用来消除那些特殊类型的警告的。Java7中引进的@SafeVarargs注解是用来指定一些方法或构造方法是安全的,这些方法或构造方法使用了不定长参数。如果传递的不定长参数是泛型的,我们可以给方法加上@SafeVarargs来消除无害的警告。

public class SafeVargExample {    @SafeVarargs    public static <T> void displayElements(T... array) {        for (T element : array) {            System.out.println(element.getClass().getName() + ": " + element);        }    }    public static void main(String[] args) {        ArrayList<Integer> a1 = new ArrayList<>();        a1.add(new Integer(1));        a1.add(2);                ArrayList<Float> a2 = new ArrayList<>();        a2.add(new Float(3.0));        a2.add(new Float(4.0));                displayElements(a1, a2, 12);    }}

读书人网 >编程

热点推荐