JAVA JDK(3)—— jdk7特性

今日一言:
She's articulate, strong, persuasive,
arugumentative, beautiful and she's
my dearest, dearest friend.
  ——《五十度灰》

JAVA JDK(3)—— jdk7特性

JDK7实用的新特性很多我都没用过是实用的。


参考资料:

目录:

  • jdk5
  • jdk6
  • jdk7
  • jdk8

JDK 6

1. 字符串switch

public static final String STRING_OK = "OK";
public static final String STRING_ERROR = "ERROR";

public static void main(String[] args) {
    String string = "OK";
    switch (string){
        case STRING_ERROR: out.println("It is Ok!"); break;
        case STRING_OK: out.println("It has error!"); break;
        default: out.println("The string is invalid!"); break;
    }
}

2. 泛型实例化类型自动推断

"<>"的作用:

ArrayList<String> strings = new ArrayList<String>();

3. 自定义自动关闭类、多种异常处理

可以在资源不用的时候自动调用,不仅可以自定义,也从手动挡切到自动挡了。

public class testJava7 implements AutoCloseable{

    @Override
    public void close() throws IOException , NullPointerException {
        out.println("I'm dead!");
    }

    public static void main(String[] args) {
        try(testJava7 test = new testJava7();
            testJava7 test2 = new testJava7()) {

            // ...

        } catch (IOException | NullPointerException e) {
            e.printStackTrace();
        }
    }

}

jdk7之前,你必须用try{}finally{}try内使用资源,在finally中关闭资源,不管try中的代码是否正常退出或者异常退出。jdk7之后,你可以不必要写finally语句来关闭资源,只要你在try的括号内部定义要使用的资源,且多个异常可以写进同一个catch里


4. 新增一些取环境信息的工具方法

没用过。


5. Boolean类型反转,空指针安全,参与位运算


6. NIO2 文件处理Files

public static void main(String[] args) throws Exception{
    List<String> list = Files.readAllLines(Paths.get("D:\\java.txt"));
    for( String line : list ){
        out.println(line);
    }
}

7. 支持二进制文字以及数值可加下划线

int binary = 0B11110000;
int i = 100_86;

猜你喜欢

转载自www.cnblogs.com/rcklos/p/12927243.html