日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java 7:项目代币中的代码示例

發布時間:2023/12/3 java 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java 7:项目代币中的代码示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
該博客通過代碼示例介紹了一些新的Java 7功能,這些功能在Project Coin一詞下進行了概述。 Project Coin的目標是向JDK 7添加一組小的語言更改。這些更改確實簡化了Java語言語法。 更少的打字,更簡潔的代碼,愉悅的開發人員; 先決條件

在計算機上安裝Java 7 SDK
安裝Eclipse Indigo 3.7.1

您需要尋找適合您的操作系統的捆綁軟件。

在Eclipse工作區中,您需要在運行時中定義已安裝的Java 7 JDK。 在工作臺中,轉到窗口>首選項> Java>已安裝的JRE,然后添加Java 7主目錄。

接下來,您需要在Java> Compiler中將編譯器級別設置為1.7。

項目幣

改進的文字

文字是固定值的源代碼表示。

“在Java SE 7和更高版本中,數字文字中數字之間的任何位置都可以出現任何數量的下劃線字符(_)。 此功能使您可以將數字文字中的數字組分開,從而可以提高代碼的可讀性。” (來自Java教程 )

public class LiteralsExample { public static void main(String[] args) { System.out.println("With underscores: "); long creditCardNumber = 1234_5678_9012_3456L; long bytes = 0b11010010_01101001_10010100_10010010; System.out.println(creditCardNumber); System.out.println(bytes); System.out.println("Without underscores: "); creditCardNumber = 1234567890123456L; bytes = 0b11010010011010011001010010010010; System.out.println(creditCardNumber); System.out.println(bytes); } }

注意文字中的下劃線(例如1234_5678_9012_3456L)。 結果寫入控制臺:

With underscores: 1234567890123456 -764832622 Without underscores: 1234567890123456 -764832622

如您所見,下劃線對值沒有影響。 它們只是用來使代碼更具可讀性。

SafeVarargs

在JDK 7之前的版本中,調用某些varargs庫方法時始終會收到未經檢查的警告。 如果沒有新的@SafeVarargs批注,此示例將創建未經檢查的警告。

public class SafeVarargsExample { @SafeVarargs static void m(List<string>... stringLists) { Object[] array = stringLists; List<integer> tmpList = Arrays.asList(42); array[0] = tmpList; // compiles without warnings String s = stringLists[0].get(0); // ClassCastException at runtime } public static void main(String[] args) { m(new ArrayList<string>()); } }</string></integer></string>

第3行中的新注釋無助于在運行時解決煩人的ClassCastException 。 而且,它只能應用于靜態方法和最終方法。 因此,我相信這不會有太大幫助。 Java的未來版本將對不安全的代碼產生編譯時錯誤,例如上例中的代碼。

鉆石

在Java 6中,需要耐心地創建地圖列表。 看這個例子:

public class DiamondJava6Example {public static void main(String[] args) {List<Map<Date, String>> listOfMaps = new ArrayList<Map<Date, String>>(); // type information twice!HashMap<Date, String> aMap = new HashMap<Date, String>(); // type information twiceaMap.put(new Date(), "Hello");listOfMaps.add(aMap);System.out.println(listOfMaps);} }

正如你可以在3和4行轉讓的右側看到你需要重復的類型信息的listOfMaps變量還有的aMap變量。 在Java 7中,這不再是必需的:

public class DiamondJava7Example {public static void main(String[] args) {List<Map<Date, String>> listOfMaps = new ArrayList<>(); // type information once!HashMap<Date, String> aMap = new HashMap<>(); // type information once!aMap.put(new Date(), "Hello");listOfMaps.add(aMap);System.out.println(listOfMaps);} }

多漁獲

在Java 7中,不需要為每個單個異常都包含catch子句,可以在一個子句中捕獲多個異常。 您記得這樣的代碼:

public class HandleExceptionsJava6Example { public static void main(String[] args) { Class string; try { string = Class.forName("java.lang.String"); string.getMethod("length").invoke("test"); } catch (ClassNotFoundException e) { // do something } catch (IllegalAccessException e) { // do the same !! } catch (IllegalArgumentException e) { // do the same !! } catch (InvocationTargetException e) { // yeah, well, again: do the same! } catch (NoSuchMethodException e) { // ... } catch (SecurityException e) { // ... } } }

從Java 7開始,您可以像這樣編寫它,這使我們的生活更加輕松:

public class HandleExceptionsJava7ExampleMultiCatch { public static void main(String[] args) { try { Class string = Class.forName("java.lang.String"); string.getMethod("length").invoke("test"); } catch (ClassNotFoundException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { // do something, and only write it once!!! } } }

switch語句中的字符串

由于Java 7可以在switch子句中使用字符串變量。 這是一個例子:

public class StringInSwitch { public void printMonth(String month) { switch (month) { case "April": case "June": case "September": case "November": case "January": case "March": case "May": case "July": case "August": case "December": default: System.out.println("done!"); } } }

資源試穿

此功能確實有助于減少意外的運行時執行。 在Java 7中,可以使用所謂的try-with-resource子句,該子句在發生異常時自動關閉所有打開的資源。 看例子:

import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; public class TryWithResourceExample { public static void main(String[] args) throws FileNotFoundException { // Java 7 try-with-resource String file1 = "TryWithResourceFile.out"; try (OutputStream out = new FileOutputStream(file1)) { out.write("Some silly file content ...".getBytes()); ":-p".charAt(3); } catch (StringIndexOutOfBoundsException | IOException e) { System.out.println("Exception on operating file " + file1 + ": " + e.getMessage()); } // Java 6 style String file2 = "WithoutTryWithResource.out"; OutputStream out = new FileOutputStream(file2); try { out.write("Some silly file content ...".getBytes()); ":-p".charAt(3); } catch (StringIndexOutOfBoundsException | IOException e) { System.out.println("Exception on operating file " + file2 + ": " + e.getMessage()); } // Let's try to operate on the resources File f1 = new File(file1); if (f1.delete()) System.out.println("Successfully deleted: " + file1); else System.out.println("Problems deleting: " + file1); File f2 = new File(file2); if (f2.delete()) System.out.println("Successfully deleted: " + file2); else System.out.println("Problems deleting: " + file2); } }

在第14行中,try-with-resource子句用于打開我們要操作的文件。 然后,第16行生成一個運行時異常。 請注意,我沒有明確關閉資源。 當您使用try-with-resource時,這是自動完成的。 當您使用第21-30行中顯示的Java 6等效項時,*不是*。

該代碼會將以下結果寫入控制臺:

Exception on operating file TryWithResourceFile.out: String index out of range: 3 Exception on operating file WithoutTryWithResource.out: String index out of range: 3 Successfully deleted: TryWithResourceFile.out Problems deleting: WithoutTryWithResource.out

就項目硬幣而言就是這樣。 在我眼中非常有用的東西。

參考:來自我們JCG合作伙伴 Niklas的“ Java 7:代碼示例中的項目代幣”。

相關文章 :

  • Java 7功能概述
  • 在Java 7中處理文件
  • 具有Java 7中自動資源管理功能的GC
  • Java 7:嘗試資源
  • Java SE 7、8、9 –推進Java

翻譯自: https://www.javacodegeeks.com/2012/01/java-7-project-coin-in-code-examples.html

總結

以上是生活随笔為你收集整理的Java 7:项目代币中的代码示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。