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); }}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(); }}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(); } }}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"); } }}List<String> list = new ArrayList<>();List<Map<String, List<String>> stringList = new ArrayList<>();
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); }}