以前面試的時候問了我是否瞭解JDK10的變化,一時回答不出來,因此只回答了JDK8中的函數式編程和流編程。今天看到這篇講JAVA10的文章,順便了解一下。java
JAVA10的全部新特性請參考這裏。在全部的JEP中,JEP-286在社區中引起了熱烈的討論。所以今天將介紹此特性。面試
在JAVA8中,咱們能夠將下面這樣使用的菱形運算符:express
List<Map> data = new ArrayList<Map>();
轉化爲:編程
List<Map> data = new ArrayList<>();
RHS上的類型經過LHS上的類型來推斷。微信
Java 10在此基礎上又向前邁進了一步:函數式編程
var data = new ArrayList<>();
局部變量類型推斷容許開發人員跳過局部變量的類型聲明(局部變量是指在方法定義,初始化塊,for循環和其它的如if-else代碼塊),JDK會推斷該局部變量的類型。函數
下面我寫一個樣例代碼來展現使用局部變量推斷var的不一樣的方法:oop
public class LegalLocalVarInferenceDemo{ //在靜態或是實例化的代碼塊中 static { var anotherName = "Sanaulla"; System.out.println("Hello, " + anotherName); } public static void main(String[] args){ //局部變量 var name = "Mohamed Sanualla"; System.out.println("Hello " + name); var data = new ArrayList<>(); data.add(Map.of("key1", "value1", "key2", "value2")); data.add(Map.of("key11", "value11", "key22", "value22")); data.add("hello"); System.out.println(data); System.out.println("********** As iteration variable in enhanced for-loop ***************"); //for循環中 for ( var object : data){ System.out.println(String.format("%s of type %s", object, object.getClass().getName())); } System.out.println("********** As looping index in for-loop ***************"); //for循環中 for ( var i = 0 ; i < data.size(); i++ ){ var object = data.get(i); System.out.println(String.format("%s of type %s", object, object.getClass().getName())); } System.out.println("********** As a return value from another method ***************"); //另外一個方法的返回值 var anInteger = someAnotherMethod(); System.out.println("someAnotherMethod returned " + anInteger); } //As a return value in a method public static Integer someAnotherMethod(){ System.out.println("someAnotherMethod called"); var returnObj = 12; return returnObj; } }
public class IllegalLocalVarInferenceDemo{ //不許聲明實例變量 //var someProperty; //不許做爲構造器的參數 // public LocalVarInferenceDemo(var param1){ // } public static void main(String[] args){ //不許被catch塊捕獲 // try{ // //some operations // }catch(var ex){ // } } //不許做爲方法聲明的參數 //public static void someMethod(var param1, var param2){ // System.out.println("Some method called"); //} //不許做爲方法的返回值 // public static var someAnotherMethod2(){ // System.out.println("someAnotherMethod called"); // var returnObj = 12; // return returnObj; // } }
Main.java:81: error: cannot infer type for local variable x var x; ^ (cannot use 'val' on variable without initializer) Main.java:82: error: cannot infer type for local variable f var f = () -> { }; ^ (lambda expression needs an explicit target-type) Main.java:83: error: cannot infer type for local variable g var g = null; ^ (variable initializer is 'null') Main.java:84: error: cannot infer type for local variable c var c = l(); ^ (inferred type is non denotable) Main.java:195: error: cannot infer type for local variable m var m = this::l; ^ (method reference needs an explicit target-type) Main.java:199: error: cannot infer type for local variable k var k = { 1 , 2 }; ^ (array initializer needs an explicit target-type)
JRE-286
想要了解更多開發技術,面試教程以及互聯網公司內推,歡迎關注個人微信公衆號!將會不按期的發放福利哦~this