---
title: "一叶观Java7之5特性"
author: "ixu"
date: "2011-11-21T12:48:49Z"
tags: []
categories: ["编程语言"]
slug: "java7-5"
draft: false
lastmod: "2011-11-21T12:48:49Z"
id: 1270645
url: "https://liuu.iteye.com/blog/1270645"
---

一叶观Java7之5特性

liuu

一个类中，尽览Java7的5个新特性，大家找找看，到底是哪几个：

```
public class ProjectCoinBanker {

  private static final Integer ONE_MILLION = 1_000_000;
  private static final String RICH_MSG = "You need more than $%,d to be considered rich.";

  public static void main(String[] args) throws Exception {
	System.out.println(String.format(RICH_MSG, ONE_MILLION));

	String requestType = args[0];
	String accountId = args[1];
	switch (requestType) {
		case "displayBalance":
			printBalance(accountId);
			break;
		case "lastActivityDate" :
			printLastActivityDate(accountId);
			break;
		case "amIRich" :
			amIRich(accountId);
			break;
		case "lastTransactions" :
			printLastTransactions(accountId, Integer.parseInt(args[2]));
			break;
		case "averageDailyBalance" :
			printAverageDailyBalance(accountId);
			break;
		default: break;
	}
  }

  private static void printAverageDailyBalance(String accountId) {
        String sql = String.format(AVERAGE_DAILY_BALANCE_QUERY, accountId);
	try (
	      PreparedStatement s = _conn.prepareStatement(sql);
              ResultSet rs = s.executeQuery();
             ) {
	        while (rs.next()) {
		  //print the average daily balance results
                }
	     } catch (SQLException e) {
		// handle exception, but no need for finally to close resources
                for (Throwable t : e.getSuppressed()) {
		   System.out.println("Suppressed exception message is " + t.getMessage());
		}
	     }
  }

  private static void printLastTransactions(String accountId, int numberOfTransactions) {
	List<Transaction> transactions = new ArrayList<>();
	... handle fetching/printing transactions
  }

  private static void printBalance(String accountId) {
	try {
		BigDecimal balance = getBalance(accountId);
		//print balance
	} catch (AccountFrozenException | ComplianceViolationException | AccountClosedException e) {
	    System.err.println("Please see your local branch for help with your account.");
	}
  }

  private static void amIRich(String accountId) {
	try {
		BigDecimal balance = getBalance(accountId);
		//find out if the account holder is rich
	} catch (AccountFrozenException | ComplianceViolationException | AccountClosedException e) {
	    System.out.println("Please see your local branch for help with your account.");
	}
  }

  private static BigDecimal getBalance(String acccountId)
      throws AccountFrozenException, AccountClosedException, ComplianceViolationException {
      ... getBalance functionality
  }

}
```

 ---------------------------------------------

这5个特性是：

1. 数字可由下划线分隔，增强可读性；
2. Switch支持字符串类型，方便啊；
3. catch可同时捕获多种异常，catch大瘦身；
4. 范型类型推断，不用重复写范型类型名了；
5. try增加自动释放资源（AutoClosable接口），忘掉嵌try...catch的finally吧；

代码原文及详细解释，见：[http://www.javacodegeeks.com/2011/11/java-7-feature-overview.html](http://www.javacodegeeks.com/2011/11/java-7-feature-overview.html)