成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

探秘JDK 7:將會出現(xiàn)新的語言特性

開發(fā) 后端
JDK 7本來在6月初將發(fā)布,但是Oracle把正式發(fā)布的時間推至今年秋天,JDK 7這個新版本將給我們帶來很多的新特性,本文將給大家介紹JDK 7中新的語言特性,重點介紹二進(jìn)制字面量,switch語句對字符串的支持和整型字面量下劃線支持。

51CTO與所有的讀者一樣都在關(guān)注“Java 7”,曾經(jīng)在6月4日跟蹤報道“JDK 7發(fā)布情況”。本文***將從語言特性的角度與大家一起探討一下JDK 7,看看JDK 會給***帶來什么樣的驚喜!

對JDK 7 的期待

Java平臺***的主要版本是2006年12月發(fā)布的Java SE 6,經(jīng)過近4年的開發(fā),下一代Java平臺將在今年與大家見面,根據(jù)OpenJDK功能列表的顯示,以下功能將會包含在JDK 7中(最有可能被稱為Java SE 7):

◆ 并發(fā)和集合更新;

◆ 橢圓曲線加密技術(shù);

◆ 前向移植Java SE 6u10部署特性:Java內(nèi)核,Quickstarter等;

◆ JAXB,JAXP和JAX-WS API升級;

◆ 新的語言特性:在任何Java類型上的注解,自動資源管理,二進(jìn)制字面量,閉包,為模塊化編程提供語言和虛擬機支持,switch語句支持字符串,泛型實例類型推斷,整型字面量下劃線支持等;

◆ 為Java SE 6u10圖形功能提供了新的平臺API:重量級/輕量級組件的混合,半透明和任意形狀的窗口;

◆ 新的Swing組件:JXDatePicker,JXLayer裝飾構(gòu)件;

◆ Swing新的Nimbus外觀;

◆ NIO.2(新的I/O,第二代);

◆ 在Solaris上支持套接字定向協(xié)議(Sockets Direct Protocol,SDP)和流控制傳輸協(xié)議(Stream Control Transmission Protocol,SCTP);

◆ Unicode 5.1支持;

◆ 升級了類加載器架構(gòu),包括了一個關(guān)閉URLClassLoader的方法;

◆ 虛擬機增強:壓縮64位對象指針,新的G1垃圾回收器,對非Java語言的支持(InvokeDynamic);

◆ 為Java 2D提供的XRender管道。

除了等待今年晚些時候的JDK 7官方發(fā)布,你也可以在其早期版本中嘗試其中的一些特性,可以去http://java.sun.com/javase/downloads/ea.jsp下載JDK 7第5個里程碑版本(目前***的版本)。

本文將重點介紹語言新特性中的二進(jìn)制字面量,在switch中使用字符串和整型字面量下劃線,我的環(huán)境是Windows XP SP3+JDK 7里程碑5版本,本文引用的示例代碼可從http://www.informit.com/content/images/art_friesen_exploringjdk1/elementLinks/code.zip打包下載。

#p#

二進(jìn)制字面量

Java從C/C++繼承了用十進(jìn)制(63),十六進(jìn)制(0x3f)和八進(jìn)制符號表示整型字面量,JDK 7也允許你增加0B或0b前綴用二進(jìn)制符號表示整型字面量,如:

  1. int x = 0b10101111;  
  2. System.out.printf ("%d%n", x); // Output: 175  
  3.  

轉(zhuǎn)換為二進(jìn)制

java.util.Formatter類中的System.out.printf()方法提供了格式轉(zhuǎn)換功能,你可以使用它將一個整數(shù)轉(zhuǎn)換成十進(jìn)制,十六進(jìn)制和八進(jìn)制符號,但它(仍然)不支持轉(zhuǎn)換成二進(jìn)制,必須借助整數(shù)的toBinaryString()方法進(jìn)行轉(zhuǎn)換:

  1. System.out.printf ("%s%n", Integer.toBinaryString (78));  
  2.  

這段代碼將輸出1001110,如果你希望Integer.toBinaryString()的輸出結(jié)果包括首位的0(這在匹配列中二進(jìn)制數(shù)字時非常有用),但不幸的是,這個方法不能滿足你的愿望,必須再尋找另外的辦法。

你可能會疑惑為什么二進(jìn)制字面量怎么會包含在JDK 7中,據(jù)這個特性的創(chuàng)始人Derek Foster講,使用按位運算的代碼更具可讀性,更容易驗證使用二進(jìn)制數(shù)字指定常量的技術(shù)規(guī)范,他同時指出,從心理上講,從二進(jìn)制轉(zhuǎn)換成十六進(jìn)制容易犯錯。

當(dāng)然,你也可以依賴整數(shù)的parseInt()方法將二進(jìn)制數(shù)字字符串轉(zhuǎn)換成整數(shù),如Integer.parseInt ("00110011", 2)將返回51,但是,由于下列原因調(diào)用這個方法會有問題:

◆ 這個方法調(diào)用比直接使用字面量更冗長,它的調(diào)用語法極其凌亂;

◆ 這個方法調(diào)用會帶來一定的性能損失;

◆ 編譯器不能內(nèi)聯(lián)這個方法調(diào)用返回的值,但可以內(nèi)聯(lián)一個常量的值;

◆ 在字符串中檢查到錯誤時,這個方法調(diào)用會拋出一個異常,我們在編譯時才能捕獲這個異常;

◆ 與二進(jìn)制字面量不一樣,不能使用switch語句的選擇器值表示一個方法調(diào)用,如case Integer.parseInt ("00001110", 2):這樣的語法是不正確的(也很丑陋),而case 0B00001110:這樣的語法就是正確的(也易于閱讀)。

#p#

在字符串上使用switch

在JDK 7中,switch語句進(jìn)行了小幅升級,現(xiàn)在可以在字符串上使用switch了,你可以給switch語句提供一個字符串表達(dá)式,也可以給每個case提供一個常量字符串表達(dá)式,清單1是一個使用這個特性的WC(字?jǐn)?shù)統(tǒng)計)程序的代碼。

清單1 WC.java

  1. // WC.java  
  2. import java.io.IOException;  
  3. public class WC  
  4. {  
  5.    public static void main (String [] args) throws IOException  
  6.    {  
  7.       boolean caseInsensitive = false;  
  8.       boolean verbose = false;  
  9.       for (String arg: args)  
  10.            switch (arg)  
  11.            {  
  12.               case "-i":  
  13.               case "-I": caseInsensitive = true;  
  14.                          break;  
  15.                       
  16.               case "-V":  
  17.               case "-v": verbose = true;  
  18.                          break;  
  19.               default  : System.err.println ("usage  : "+  
  20.                                              "java WC [-i|-I -v|-V] stdin");  
  21.                          System.err.println ("example: java WC -v <WC.java");  
  22.                          return;  
  23.            }  
  24.       if (verbose)  
  25.           countWordsVerbose (caseInsensitive);  
  26.       else  
  27.           countWords ();  
  28.    }  
  29.    static void countWords () throws IOException  
  30.    {  
  31.       int ch, nWords = 0;  
  32.       while ((ch = System.in.read ()) != -1)  
  33.       {  
  34.          if (Character.isLetter (ch)) // Start of word is indicated by letter.  
  35.          {  
  36.              do  
  37.              {  
  38.                 ch = System.in.read ();  
  39.              }  
  40.              while (Character.isLetterOrDigit (ch));  
  41.              nWords++;  
  42.          }  
  43.       }  
  44.       System.out.println ("\nTotal words = " + nWords);  
  45.    }  
  46.    static void countWordsVerbose (boolean caseInsensitive) throws IOException  
  47.    {  
  48.       int ch;  
  49.       WordNode root = null;  
  50.       while ((ch = System.in.read ()) != -1)  
  51.       {  
  52.          if (Character.isLetter (ch)) // Start of word is indicated by letter.  
  53.          {  
  54.              StringBuffer sb = new StringBuffer ();  
  55.              do  
  56.              {  
  57.                 sb.append ((char) ch);  
  58.                 ch = System.in.read ();  
  59.              }  
  60.              while (Character.isLetterOrDigit (ch));  
  61.              if (root == null)  
  62.                  root = new WordNode (sb.toString ());  
  63.              else  
  64.                  root.insert (sb.toString (), caseInsensitive);  
  65.          }  
  66.       }  
  67.       display (root);  
  68.    }  
  69.    static void display (WordNode root)  
  70.    {  
  71.       // root == null when leaf node has been reached (or perhaps there are no  
  72.       // words in tree)  
  73.       if (root == null)  
  74.           return;  
  75.       // Display all words lexicographically less than the word in the current  
  76.       // node.  
  77.       display (root.left);  
  78.       // Display current node's word and number of occurrences.  
  79.       System.out.println ("Word = " + root.word + "Count = " +  
  80.                           root.count);  
  81.       // Display all words lexicographically greater than the word in the  
  82.       // current node.  
  83.       display (root.right);  
  84.    }  
  85. }  
  86. class WordNode  
  87. {  
  88.    String word;    // Stored word  
  89.    int count = 1;  // Number of occurrences of word in text  
  90.    WordNode left;  // Left subtree  
  91.    WordNode right; // Right subtree  
  92.    public WordNode (String word)  
  93.    {  
  94.       this.word = word;  
  95.       left = right = null;  
  96.    }  
  97.    public void insert (String word, boolean caseInsensitive)  
  98.    {  
  99.       int order = (caseInsensitive) ? this.word.compareToIgnoreCase (word)  
  100.                                     : this.word.compareTo (word);  
  101.       if (order > 0) // word argument lexicographically less than current  
  102.                      // word  
  103.       {  
  104.           // If left-most leaf node reached then insert new node as its  
  105.           // left-most leaf node; otherwise, keep searching left.  
  106.           if (left == null)  
  107.               left = new WordNode (word);  
  108.           else  
  109.               left.insert (word, caseInsensitive);  
  110.       }  
  111.       else  
  112.       if (order < 0) // word argument lexicographically greater than current  
  113.                      // word  
  114.       {  
  115.           // If right-most leaf node reached then insert new node as its  
  116.           // right-most leaf node; otherwise, keep searching right.  
  117.           if (right == null)  
  118.               right = new WordNode (word);  
  119.           else  
  120.               right.insert (word, caseInsensitive);  
  121.       }  
  122.       else  
  123.           this.count++; // Update number of found occurrences.  
  124.    }  
  125. }  
  126.  

#p#

上面的例子充分說明了處理命令行參數(shù)時在字符串上使用switch是很有用的,可以替代這個功能的是if-else if … else表達(dá)式,但這樣一來會使代碼更冗長。

編譯好WC.java后,指定(-i或I,區(qū)分大小寫)和(-v或-V,輸出詳細(xì)信息)命令行參數(shù)運行這個程序,如:

  1. java WC <WC.java        // Count the number of words in WC.java and report the total.  
  2. java WC -v <WC.java     // Count the number of occurrences of each word in WC.java and report   
  3.                         // each total.  
  4. java WC -i -v <WC.java  // Count the number of occurrences of each word in WC.java and report   
  5.                         // each total. Use a case-insensitive comparison so that, for example,   
  6.                         // this and This are treated as two occurrences of the same word instead   
  7.                         // of one occurrence each of two different words.  

整型字面量下劃線

JDK 7支持?jǐn)?shù)字下劃線,改善了二進(jìn)制,十進(jìn)制,十六進(jìn)制和八進(jìn)制字面量的可讀性,如:

  1. int mb_directory_info = 204_555_1212;  
  2. System.out.printf ("%d%n", mb_directory_info); // Output: 2045551212  
  3. long debt = 11_000_000_000_000L;  
  4. System.out.printf ("%d%n", debt); // Output: 11000000000000  
  5. byte max_pos_value = 0x0___07F;  
  6. System.out.printf ("%d%n", max_pos_value); // Output: 127  
  7.  

你可以在連續(xù)數(shù)字之間插入一到多個下劃線,但不能在數(shù)字的最前面指定下劃線(如_25這樣是不允許的),因為這樣將被解釋為一個標(biāo)識符,同樣,也不能用下劃線作為后綴(如0x3f_這樣也是不允許的)。

雖然Foster提到Integer和Long的decode()方法將支持這個特性,但目前的版本還不支持,同樣,Integer.parseInt()和Long.parseLong()也不支持這個特性。

小結(jié)

二進(jìn)制字面量,switch對字符串的支持和整型字面量下劃線支持僅僅是JDK 7新語言特性的一小部分,可以說它們是小而強大,但與閉包和模塊化比起來,很多人可能會覺得它們微不足道。下一篇文章將會介紹起源于Java SE 6的半透明和任意形狀的窗口的改進(jìn)。

【編輯推薦】

  1. Java 7,一個技術(shù)標(biāo)準(zhǔn)的商業(yè)咒語
  2. JDK 7功能完備版今天發(fā)布?
  3. JDK 7一再延期的背景披露以及OpenJDK的尷尬
責(zé)任編輯:佚名 來源: IT168
相關(guān)推薦

2011-07-29 09:31:32

JDK 7

2011-05-20 09:35:22

JDK7

2011-05-20 09:43:23

JDK7

2010-06-28 09:26:15

JDK 7Swing組件Java

2011-05-20 09:53:00

JDK7

2011-05-20 10:15:06

JDK7

2011-05-20 10:28:29

JDK7

2011-05-20 10:20:11

JDK7

2011-05-20 09:59:42

JDK7

2009-11-23 19:50:12

PHP6.0

2014-11-26 10:23:09

2012-08-07 09:37:23

虛擬化

2017-08-08 10:30:50

物聯(lián)網(wǎng)信息安全網(wǎng)絡(luò)

2021-05-06 20:03:00

JavaStream代碼

2015-10-23 11:35:00

軟件開發(fā)發(fā)展趨勢

2015-10-27 15:42:57

軟件開發(fā)發(fā)展趨勢

2009-07-07 12:30:38

JDK1.6

2009-07-09 16:29:19

JDK版本

2019-09-05 09:38:36

2021-04-28 16:08:41

物聯(lián)網(wǎng)趨勢物聯(lián)網(wǎng)IOT
點贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 亚洲a在线视频 | 日本韩国电影免费观看 | 高清视频一区二区三区 | 91久久久久久久久久久久久 | 一级毛片视频 | 久久蜜桃资源一区二区老牛 | 五月婷婷婷| 综合久 | 国产成人99久久亚洲综合精品 | 精品一区二区三区免费视频 | 999国产精品视频免费 | 伊人免费观看视频 | 热久久久 | 91精品国产乱码久久蜜臀 | 精品久久久久久国产 | 一区二区三区视频在线 | 特级一级黄色片 | av在线成人 | 国产高清美女一级a毛片久久w | 欧美高清免费 | 天天澡天天狠天天天做 | 亚洲97| 日韩看片 | 伊人网站 | 国产网站在线免费观看 | 亚洲超碰在线观看 | 中文字幕欧美日韩一区 | 亚洲一区电影 | 国产精品美女 | hsck成人网| 黄色网址在线播放 | 日韩成人在线免费视频 | 日韩视频在线一区 | 在线国产一区 | 欧美在线观看一区 | 久久成人免费视频 | 九色 在线| 亚洲欧美一区二区三区视频 | 亚欧性视频 | 久久久免费毛片 | 日韩欧美国产一区二区三区 |