技术中心

这里象征着我们的态度和能力

>Java 7语言的新特性
发布者:中国IT实验室    信息来源:中国IT实验室    发布时间:2012-01-09      浏览次数:4952
分享到:

新浪微博

腾讯微博

QQ空间

豆瓣网

QQ好友

欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入

    1. 新的二进制数字的表达方式

    如下面的二进制数常量

    byte b = (byte)0b00010011;

    short s = (byte)0b001000000000000;

    http://download.oracle.com/javase/7/docs/technotes/guides/language/binary-literals.html

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

    long creditCardNumber = 1234_5678_9012_3456L;

    long socialSecurityNumber = 999_99_9999L;

    float pi = 3.14_15F;

    long hexBytes = 0xFF_EC_DE_5E;

    long hexWords = 0xCAFE_BABE;

    long maxLong = 0x7fff_ffff_ffff_ffffL;

    byte nybbles = 0b0010_0101;

    long bytes = 0b11010010_01101001_10010100_10010010

    非法的使用方式

    float pi1 = 3_.1415F; // Invalid; cannot put underscores adjacent to a decimal point

    float pi2 = 3._1415F; // Invalid; cannot put underscores adjacent to a decimal point

    long socialSecurityNumber1 = 999_99_9999_L;

    http://download.oracle.com/javase/7/docs/technotes/guides/language/underscores-literals.html

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

    public String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) {

    String typeOfDay;

    switch (dayOfWeekArg) {

    case "Monday":

    typeOfDay = "Start of work week";

    break;

    case "Tuesday":

    case "Wednesday":

    case "Thursday":

    typeOfDay = "Midweek";

    break;

    case "Friday":

    typeOfDay = "End of work week";

    break;

    case "Saturday":

    case "Sunday":

    typeOfDay = "Weekend";

    break;

    default:

    throw new IllegalArgumentException("Invalid day of the week: " +

    dayOfWeekArg);

    }

    return typeOfDay;

    }

    http://download.oracle.com/javase/7/docs/technotes/guides/language/strings-switch.html

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

    //Java 7之前

    Map<String, List<String》 myMap = new HashMap<String, List<String》();

    //Java 7

    Map<String, List<String》 myMap = new HashMap<>();

    <>符号是必须的。

    http://download.oracle.com/javase/7/docs/technotes/guides/language/type-inference-generic-instance-creation.html

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

    http://download.oracle.com/javase/7/docs/technotes/guides/language/non-reifiable-varargs.html

    6. try-with-resources语句

    public static void viewTable(Connection con) throws SQLException {

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

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

    ResultSet rs = stmt.executeQuery(query);

    while (rs.next()) {

    String coffeeName = rs.getString("COF_NAME");

    int supplierID = rs.getInt("SUP_ID");

    float price = rs.getFloat("PRICE");

    int sales = rs.getInt("SALES");

    int total = rs.getInt("TOTAL");

    System.out.println(coffeeName + ", " + supplierID + ", " + price +

    ", " + sales + ", " + total);

    }

    } catch (SQLException e) {

    JDBCTutorialUtilities.printSQLException(e);

    }

    }

    http://download.oracle.com/javase/7/docs/technotes/guides/language/try-with-resources.html

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

    catch (IOException|SQLException ex) {

    logger.log(ex);

    throw ex;

    }

4000-880-989
(24小时热线)
联系客服
微信公众号

官方公众号

小程序

©2008-2022 CORPORATION ALL Rights Reserved. 昆明奥远科技有限公司版权所有 滇ICP备09003328号-1 滇公网安备 53011102000818号
昆明那家网络公司好,新媒体运营,网站优化,网络推广,网站建设,网页设计,网站设计,网站推广,云南网站公司,昆明新媒体公司,云南网红主播,昆明SEO公司,昆明网站建设,昆明网络推广,昆明网站优化,昆明网站推广,红河网站建设,大理网络公司,曲靖网络公司,丽江网站设计,昭通网络公司,保山大数据服务,智慧高速建设,智慧校园服务,云南IDC服务商,网络安全测评,等保测评,网站关键词排名优化服务,服务客户尽超2000余家,一切尽在奥远科技,服务电话:13888956730