jdk1.8 新特征

1.jdk7语法上

1.1二进制变量的表示,支持将整数类型用二进制来表示,用0b开头。

// A 16-bit 'short' value:
short aShort = (short) 0b1010000101000101;

1.2 Switch语句支持string类型

1.3 Try-with-resource语句

注意:实现java.lang.AutoCloseable接口的资源都可以放到try中,跟final里面的关闭资源类似; 按照声明逆序关闭资源 ;Try块抛出的异常通过Throwable.getSuppressed获取

try (java.util.zip.ZipFile zf = new java.util.zip.ZipFile(zipFileName);
java.io.BufferedWriter writer = java.nio.file.Files 
.newBufferedWriter(outputFilePath, charset)) {
// Enumerate each entry
for (java.util.Enumeration entries = zf.entries(); entries
.hasMoreElements();) {
// Get the entry name and write it to the output file
String newLine = System.getProperty("line.separator");
String zipEntryName = ((java.util.zip.ZipEntry) entries
.nextElement()).getName() + newLine;
writer.write(zipEntryName, 0, zipEntryName.length());
}
}

1.4 Catch多个异常 说明:Catch异常类型为final; 生成Bytecode 会比多个catch小; Rethrow时保持异常类型

public static void main(String[] args) throws Exception {
try {
testthrows();
} catch (IOException | SQLException ex) {
throw ex;
}
}
public static void testthrows() throws IOException, SQLException {
}

1.5 数字类型的下划线表示 更友好的表示方式,不过要注意下划线添加的一些标准,可以参考下面的示例

long creditCardNumber = 1234_5678_9012_3456L;
long socialSecurityNumber = 999_99_9999L;
float pi = 3.14_15F;

1.6 泛型实例的创建可以通过类型推断来简化 可以去掉后面new部分的泛型类型,只用<>就可以了。

  1. JDBC 4.1

2.1.可以使用try-with-resources自动关闭Connection, ResultSet, 和 Statement资源对象

2.2. RowSet 1.1:引入RowSetFactory接口和RowSetProvider类,可以创建JDBC driver支持的各种 row sets,这里的rowset实现其实就是将sql语句上的一些操作转为方法的操作,封装了一些功能。

2.3. JDBC-ODBC驱动会在jdk8中删除

  1. 并发工具增强

3.1.fork-join
最大的增强,充分利用多核特性,将大问题分解成各个子问题,由多个cpu可以同时解决多个子问题,最后合并结果,继承RecursiveTask,实现compute方法,然后调用fork计算,最后用join合并结果。

class Fibonacci extends RecursiveTask<Integer> {
final int n;
Fibonacci(int n) {
this.n = n;
}
private int compute(int small) {
final int[] results = { 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 };
return results[small];
}
public Integer compute() {
if (n <= 10) {
return compute(n);
}
Fibonacci f1 = new Fibonacci(n - 1);
Fibonacci f2 = new Fibonacci(n - 2);
System.out.println("fork new thread for " + (n - 1));
f1.fork();
System.out.println("fork new thread for " + (n - 2));
f2.fork();
return f1.join() + f2.join();
}
} 
  1. Jdk8 lambda表达式 最大的新增的特性,不过在很多动态语言中都已经原生支持。JAVA也能进行简单的“函数式编程”。 它是一个匿名函数,好处就是代码量大大减少了!程序逻辑也很清晰明了。 它的用处浅显来说就是替代“内部匿名类”、可以对集合或者数组进行循环操作。
    原来这么写:

    btn.setOnAction(new EventHandler() {
    @Override
    public void handle(ActionEvent event) {
    System.out.println(“Hello World!”);
    }
    });

jdk8直接可以这么写:

btn.setOnAction( 
    event -> System.out.println("Hello World!") 
);   

更多示例:

public class Utils { 
    public static int compareByLength(String in, String out){ 
        return in.length() - out.length(); 
    } 
} 

public class MyClass { 
    public void doSomething() { 
        String[] args = new String[] {"microsoft","apple","linux","oracle"} 
        Arrays.sort(args, Utils::compareByLength); 
    }  
}  

5.接口可以有静态方法,默认方法,也就是说接口中有了实现的方法

猜你喜欢

转载自blog.csdn.net/jcsyl_mshot/article/details/80436406