java 7、8、9、10、11、12新特性

Java7新特性:

Java8新特性:

Java9新特性:

Java10新特性:

Java11新特性:

Java12新特性:

 

Java7新特性:

1:switch中可以使用字符串

String s = "aa";
switch (s) {
    case "aa" :
        System.out.println("aa");
    case "aaa" :
        System.out.println("aaa");
        break ;
    default :
        System.out.println("break");
        break ;
}

 

2:砖[zhuān]石语法,泛型只需要写左边就可以

Map<String, String> map = new HashMap<>();

 

3:数字可以使用下划线

long creditCardNumber = 1234_5678_9012_3456L;
long socialSecurityNumber = 999_99_9999L;
float pi =  3.14_159F;
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;

下划线只能出现在数字之间,下面的情形不能出现下划线:

  • 数字的开头和结尾
  • 在浮点数中与小数点相邻
  • F或者L后缀之前
  • 在预期数字串的位置

例如:

float pi1 = 3_.1415F;      // 无效; 不能和小数点相邻
float pi2 = 3._1415F;      // 无效; 不能和小数点相邻
long socialSecurityNumber1 = 999_99_9999_L;         // 无效; 不能放在L后缀之前

int x1 = _52;              // 无效;这是个标识符,不是数字的字面量
int x2 = 5_2;              // OK
int x3 = 52_;              // 无效; 不能放在数字的结尾
int x4 = 5_______2;        // OK

int x5 = 0_x52;            // 无效; 不能放在 0x 中间 
int x6 = 0x_52;            // 无效; 不能放在数字的开头
int x7 = 0x5_2;            // OK
int x8 = 0x52_;            // 无效; 不能放在数字的结尾

int x9 = 0_52;             // OK 
int x10 = 05_2;            // OK
int x11 = 052_;            // Invalid; 不能放在数字的结尾

 

4:改进异常处理,一个catch可以写多个异常,用“|”分隔

try {
    ......
} catch(ClassNotFoundException|SQLException ex) {
    ex.printStackTrace();
}

  

5:TWR写法(try-with-resources)资源自动管理

  jdk7之前需要在finally中手动关闭打开的资源

InputStream is = null;
try {
    is = new FileInputStream(new File(""));
} catch (FileNotFoundException e) {
    e.printStackTrace();
} finally {
    try {
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

  jdk7之后,使用TWR写法。注意,try(这里创建的对象,必须实现了AutoCloseable接口,这个接口有close方法,这个方法会执行关闭操作,所以流是这样被关闭的) {} catch(){}

File file = new File("");
try (InputStream is = new FileInputStream(file)) {
    
} catch (IOException e) {
 
}

 

6:自定义自动关闭类

  以下是jdk7 api中的接口,(不过注释太长,删掉了close()方法的一部分注释)

/**
 * A resource that must be closed when it is no longer needed.
 *
 * @author Josh Bloch
 * @since 1.7
 */
public interface AutoCloseable {
    /**
     * Closes this resource, relinquishing any underlying resources.
     * This method is invoked automatically on objects managed by the
     * {@code try}-with-resources statement.
     *
     */
    void close() throws Exception;
}

  只要实现该接口,在该类对象销毁时自动调用close方法,你可以在close方法关闭你想关闭的资源,例子如下

class TryClose implements AutoCloseable {

 @Override
 public void close() throw Exception {
  System.out.println(" Custom close method …
                                         close resources ");
 }
}
//请看jdk自带类BufferedReader如何实现close方法(当然还有很多类似类型的类)

  public void close() throws IOException {
        synchronized (lock) {
            if (in == null)
                return;
            in.close();
            in = null;
            cb = null;
        }
    }

  

 

7:新增加了Objects类

  它提供了一些方法来操作对象,这些方法大多数是“空指针”安全的,比如 Objects.toString(obj);如果obj是null,,则输出是null。

否则如果你自己用obj.toString(),如果obj是null,则会抛出NullPointerException.

posted @ 2019-07-23 12:15  面向bug编程  阅读(403)  评论(0)    收藏  举报