一叶观Java7之5特性
一叶观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个特性是:
代码原文及详细解释,见:http://www.javacodegeeks.com/2011/11/java-7-feature-overview.html