国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

JDK1.7/1.8新特性詳解

2019-11-08 02:24:39
字體:
來源:轉載
供稿:網(wǎng)友

                            一.JDK 1.7新特性        

1.1 異常處理改進

   try-with-resources語句

[java] view plain copypublic class Test {      public static void main(String[] args) {          try(FileInputStream fin=new FileInputStream("res.txt"))//放在這其中的資源必須繼承AutoCloseable才可以          {          int x;          while((x=fin.read())!=-1)          {          System.out.PRint((char)x);            }}           catch (IOException e) {e.printStackTrace();}}}  

  捕獲多個異常

[java] view plain copytry   {          }   catch(A|B|C ex)   {          }   //只有當A,B,C沒有繼承關系時才可以,A,B,C默認是常量。  

[java] view plain copypublic class Test {      public Test(int x) throws A, B      {      if(x==0)      throw  new A("異常A");      else          throw  new B("異常B");            }      public static void main(String[] args) {               try {          Test t=new Test(2);      } catch (A |B e) {         e.printStackTrace();      }        }      }       class A extends Exception  {  public A(String str)  {  System.out.println(str);  }  }  class B extends Exception  {  public B(String str)  {  System.out.println(str);  }  }  

 使反射方法的異常處理簡單化

 在反射方法的方法中可能拋出多個異常,當然你可以使用捕獲多個異常的方法,但是還有一種更簡單的方法是JDK1.7為次提供了一個新的父類異常接口:ReflectiveOperationException。

1.2 使用文件的改進

     Path

[java] view plain copypublic class Test {         public static void main(String[] args) throws IOException {          String fileSource="in.txt";          String fileTo =null;          String path=System.getProperty("user.dir");          File dir=Paths.get(path).toFile();          File[] file=dir.listFiles();          for(File f:file)          {            if(f.getName().equalsIgnoreCase(fileSource)){          fileTo=fileSource.substring(0, fileSource.lastIndexOf('.'))+"-副本"+(int)(Math.random()*9+1)+fileSource.substring(fileSource.lastIndexOf('.'), fileSource.length());           break;           }           }          Files.copy(Paths.get(fileSource), Paths.get(fileTo));          System.out.println(fileTo);      }      }  

[java] view plain copypublic class Test {         public static void main(String[] args)  {                     Path src=Paths.get(System.getProperty("user.dir"));          Path dst=Paths.get("in.txt");          Path path0=src.resolve(dst);          System.out.println(path0);//C:/Users/Administrator/Desktop/文件/Struct/in.txt          Path path1=dst.resolve(src);          System.out.println(path1);//C:/Users/Administrator/Desktop/文件/Struct          Path path2=src.resolveSibling(dst);          System.out.println(path2);//C:/Users/Administrator/Desktop/文件/Struct          Path path3=dst.resolveSibling(src);          System.out.println(path3);//C:/Users/Administrator/Desktop/文件/Struct      }      }  

  讀取文件

[java] view plain copypublic class Test {         public static void main(String[] args) throws IOException  {                     Path src=Paths.get("in.txt");          byte[] data=Files.readAllBytes(src);          String str=new String(data);          System.out.println(str);          Path dst=Paths.get("out.txt");          String rpc="1234567890";          Files.write(dst,rpc.getBytes());                }  }  

  創(chuàng)建文件和目錄

[java] view plain copypublic class Test {      public static void main(String[] args) throws IOException  {          Path p=Paths.get(System.getProperty("user.dir")+"//RPC");          Path p1=Paths.get(System.getProperty("user.dir")+"//RPC//RGB");          Files.createDirectory(p);//中間目錄必須存在          Files.createDirectories(p1);//根目錄存在即可       }  }  

  復制、移動和刪除文件

[java] view plain copypublic class Test {      public static void main(String[] args) throws IOException  {          Path p=Paths.get("in.txt");          Path p1=Paths.get("out.txt");          Path src=Paths.get(System.getProperty("user.dir"));          Path dst=Paths.get(System.getProperty("user.dir")+"//RPC");          Files.copy(p, p1,StandardCopyOption.REPLACE_EXISTING);          Files.move(p,dst,StandardCopyOption.REPLACE_EXISTING);          Files.delete(p);           }  }  

1.3 實現(xiàn)equals、hashcode、compareTo

    安全的NULL值測試

[java] view plain copyreturn Objects.equals(first, other.first)&&Objects.equals(last, other.last)  

   計算哈希碼

[java] view plain copyObjects.hashCode(o);  Objects.hash(values);  

   比較數(shù)值類型對象

[java] view plain copyInteger.compare(x, y)  Long.compare(x, y)//還有其他基本類型  

1.4 基礎改建

    Switch 語句中允許使用 String 類型
[java] view plain copyswitch(str)   {   case "1" :   case "2":      .      .       .   case "n":   }    二進制常量和數(shù)字常量[java] view plain copyint x=0b010101;     int y=0x1234567A;     int z=0111111;           泛型實例化類型自動推斷

[java] view plain copyList<String>list=new ArrayList<>();  

  數(shù)值可加下劃線

[java] view plain copyint x=1_2_3;   float x=1_.0;//錯誤;   int z=1_f_f;/錯誤;  

1.5 其他改動

將字符串轉換為數(shù)字

[java] view plain copyint x=Integer.parseInt("+1");//JDK 1.7之前不行  

全局Logger

Logger.getGlobal()

Null檢查

Objects.requireNonNull(Object obj)

                           

                           二.JDK1.8新特性

2.1 lambda表達式

條件:函數(shù)式接口:最多只能包含一個抽象函數(shù)聲明。可以用@FunctionalInterface注解來標識是否滿足函數(shù)式接口,如果滿足以函數(shù)式接口規(guī)范也可不需用注解來標識。

注意:在lambda表達式中出現(xiàn)異常,要么捕獲處理,要么將該表達式賦值給能拋出相同異常的函數(shù)式接口。

重點:lambda表達式所接受的對象絕對不能是泛型類型,如果你的函數(shù)式接口的方法沒有聲明異常類型,那么你在使用lambda表達式的時候就只能捕獲異常并處理,lambda表達式的使用要上下文推斷,不能隨便使用,否則會出現(xiàn)錯誤,被lambda表達式捕獲的變量到lambda表達式中全部為常量,例如你在lambda表達式定義的上下文聲明定義了一個非常量型的值i=0;那么一旦它被lambda表達式捕獲之后,在lambda表達式中,它變成常量,不能改變。不能在lambda表達式所定義的上下文定義和lambda表達式中相同的變量。不能重復定義lambda表達式的參數(shù)類型

[java] view plain copypublic static void main(String[] args) {           int i=0;           //A a=()->{int i=0;}; 錯誤,不能在lambda表達式定義上下文重復變量          //A a=()->{i=2;};  錯誤,不能在lambda表達式更改捕獲變量的值        }  [java] view plain copy/*    * lambda表達式的匹配規(guī)則:    * 1.參數(shù)的類型要一致    * 2.返回值類型要一致    * 3.符合兼容規(guī)則:自動拆箱裝修功能;符合寬化處理,例如:byte->short,如果窄化處理時,需強制轉換;存在類的繼承關系也可以。    */  [java] view plain copypublic interface A<T> {      public T RGB(T t) throws Exception;    }  A<String>a=name->{return name;};          System.out.println(a.RGB("1234"));  [java] view plain copypublic interface A {      public <T>T RGB(T t) throws Exception;    }  public class Test {        public static void main(String[] args) throws Exception {       /*       * lamdba 表達式對象只能接收具體類型對象       */              //A a=name->{return name;};  //會提示:Illegal lambda expression: Method RGB of type A is generic           A a=Test::getString;    //這種形式可以              System.out.println(a.RGB(new Object()));      }  @SuppressWarnings("unchecked")  public static <T> T getString(T str)  {  return (T) str.toString().toUpperCase();  }  }  

相關知識:

方法引用

對象::實例方法

類::實例方法

類::靜態(tài)方法

[java] view plain copypublic interface Function {  public void getName(Object o);  }  [java] view plain copypublic class Test {        public static void main(String[] args) {          Function func=System.out::println;          func.getName("123");      }    }  

構造器引用

類::new即可表示一個構造器引用

變量作用域

利用lambda表達式創(chuàng)造的相當于方法里面的內部類,所用的參數(shù)默認為常量型。

默認方法

可以在接口中用default聲明一個方法,該方法可以有方法實體,

接口中的靜態(tài)方法

接口中同時也也可以有static修飾的方法。

[java] view plain copypublic interface A {      public <T>T RGB(T t) throws Exception;      public default void s()      {                }      public static void r()      {                }    }  

2.2 Sream 常識

Stream自己不會存儲元素,元素存儲在底層集合中

Stream操作符不會改變源對象

Stream操作符可能是延遲執(zhí)行

Stream 操作流水線:1.創(chuàng)建一個Stream,2.在一個或多個步驟中,指定將初始Stream轉換為另一個Stream的中間操作。3.使用一個終止操作來產(chǎn)生一個結果。該操作會強制它之前的的延遲操作立即執(zhí)行,在這之后,Stream就不會被執(zhí)行。

2.3 JavaFX

[java] view plain copypublic class FX extends application {      @Override      public void start(Stage arg0) throws Exception {       Label label=new Label();       label.setText("中華人民共和國");       label.setFont(new Font(100));       label.autosize();       arg0.setScene(new Scene(label));       arg0.setTitle("國家");       arg0.centerOnScreen();       arg0.show();         }  }  [java] view plain copypublic static void main(String[] args) {          FX fx=new FX();          fx.launch(FX.class,"1234");      }    }  


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 石渠县| 西峡县| 齐河县| 色达县| 龙泉市| 晋城| 义马市| 赤城县| 麻城市| 谢通门县| 辛集市| 成都市| 左贡县| 颍上县| 唐山市| 海城市| 池州市| 深圳市| 兴文县| 沙坪坝区| 防城港市| 赣榆县| 浪卡子县| 十堰市| 上饶市| 马鞍山市| 闽清县| 新晃| 新巴尔虎右旗| 沁阳市| 鹿泉市| 商都县| 陆河县| 临高县| 石河子市| 常山县| 贵溪市| 红安县| 海门市| 米林县| 个旧市|