首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

一叶望Java7之5特性

2012-09-20 
一叶观Java7之5特性一叶观Java7之5特性liuu一个类中,尽览Java7的5个新特性,大家找找看,到底是哪几个:publi

一叶观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个特性是:


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


代码原文及详细解释,见:http://www.javacodegeeks.com/2011/11/java-7-feature-overview.html


热点排行