一叶观Java7之5特性

一个类中,尽览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吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值