Chinaunix首页 | 论坛 | 博客
  • 博客访问: 657700
  • 博文数量: 45
  • 博客积分: 931
  • 博客等级: 准尉
  • 技术积分: 590
  • 用 户 组: 普通用户
  • 注册时间: 2005-04-17 13:27
文章分类

全部博文(45)

文章存档

2013年(6)

2012年(15)

2011年(23)

2005年(1)

分类: Java

2011-11-22 10:29:06

1. 新的二进制数字的表达方式
如下面的二进制数常量
  1. byte b = (byte)0b00010011;
  2. short s = (byte)0b001000000000000;


2. 使用下划线对数字进行分隔

  1. long creditCardNumber = 1234_5678_9012_3456L;
  2. long socialSecurityNumber = 999_99_9999L;
  3. float pi = 3.14_15F;
  4. long hexBytes = 0xFF_EC_DE_5E;
  5. long hexWords = 0xCAFE_BABE;
  6. long maxLong = 0x7fff_ffff_ffff_ffffL;
  7. byte nybbles = 0b0010_0101;
  8. long bytes = 0b11010010_01101001_10010100_10010010

非法的使用方式

  1. float pi1 = 3_.1415F; // Invalid; cannot put underscores adjacent to a decimal point
  2. float pi2 = 3._1415F; // Invalid; cannot put underscores adjacent to a decimal point
  3. long socialSecurityNumber1 = 999_99_9999_L;


3. Switch语句支持字符串变量

  1. public String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) {
  2.     String typeOfDay;
  3.     switch (dayOfWeekArg) {
  4.         case "Monday":
  5.             typeOfDay = "Start of work week";
  6.             break;
  7.         case "Tuesday":
  8.         case "Wednesday":
  9.         case "Thursday":
  10.             typeOfDay = "Midweek";
  11.             break;
  12.         case "Friday":
  13.             typeOfDay = "End of work week";
  14.             break;
  15.         case "Saturday":
  16.         case "Sunday":
  17.             typeOfDay = "Weekend";
  18.             break;
  19.         default:
  20.             throw new IllegalArgumentException("Invalid day of the week: " +
  21.                                                 dayOfWeekArg);
  22.     }
  23.     return typeOfDay;
  24. }



4.泛型实例创建时的类型推断

  1. //Java 7之前
  2. Map<String, List<String>> myMap = new HashMap<String, List<String>>();

  3. //Java 7
  4. Map<String, List<String>> myMap = new HashMap<>();

<>符号是必须的。


5. 改进了使用可变参数时的编译器的错误和警告信息。


6. try-with-resources语句
  1. public static void viewTable(Connection con) throws SQLException {

  2.     String query = "select COF_NAME, SUP_ID, PRICE, SALES, TOTAL from COFFEES";

  3.     try (Statement stmt = con.createStatement()) {

  4.       ResultSet rs = stmt.executeQuery(query);

  5.       while (rs.next()) {
  6.         String coffeeName = rs.getString("COF_NAME");
  7.         int supplierID = rs.getInt("SUP_ID");
  8.         float price = rs.getFloat("PRICE");
  9.         int sales = rs.getInt("SALES");
  10.         int total = rs.getInt("TOTAL");
  11.         System.out.println(coffeeName + ", " + supplierID + ", " + price +
  12.                            ", " + sales + ", " + total);
  13.       }

  14.     } catch (SQLException e) {
  15.       JDBCTutorialUtilities.printSQLException(e);
  16.     }
  17.   }


7. 捕获多个异常,并重新抛出改进类型的异常。

  1. catch (IOException|SQLException ex) {
  2.     logger.log(ex);
  3.     throw ex;
  4. }


阅读(2344) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~