上一题下一题
跳转到
 
 
  世界如此多姿,发展如此迅速,窥一斑未必还能知全豹。但正如万花筒一样,每一个管窥都色彩斑斓。  
 
 
  知识通道 | 学习首页 | 教师首页 | PK首页 | 知识创造首页 | 企业首页 | 登录
 
本文对应知识领域
JDK 7探秘:新的语言特性
作者:黄永兵 申领版权
2010年07月18日 共有 2082 次访问 【添加到收藏夹】 【我要附加题目
受欢迎度:
    导读: Oracle预计会在今年秋天正式发布JDK 7,这个新版本将给我们带来很多的新特性,本文是本系列四篇文章中的第一篇,将给大家介绍JDK 7中新的语言特性,重点介绍二进制字面量,switch语句对字符串的支持和整型字面量下划线支持。
    对JDK 7 的期待
    Java平台最新的主要版本是2006年12月发布的Java SE 6,经过近4年的开发,下一代Java平台将在今年与大家见面,根据OpenJDK功能列表的显示,以下功能将会包含在JDK 7中(最有可能被称为Java SE 7):
    · 并发和集合更新;
    · 椭圆曲线加密技术;
    · 前向移植Java SE 6u10部署特性:Java内核,Quickstarter等;
    · JAXB,JAXP和JAX-WS API升级;
    · 新的语言特性:在任何Java类型上的注解,自动资源管理,二进制字面量,闭包,为模块化编程提供语言和虚拟机支持,switch语句支持字符串,泛型实例类型推断,整型字面量下划线支持等;
    · 为Java SE 6u10图形功能提供了新的平台API:重量级/轻量级组件的混合,半透明和任意形状的窗口;
    · 新的Swing组件:JXDatePicker,JXLayer装饰构件;
    · Swing新的Nimbus外观;
    · NIO.2(新的I/O,第二代);
    · 在Solaris上支持套接字定向协议(Sockets Direct Protocol,SDP)和流控制传输协议(Stream Control Transmission Protocol,SCTP);
    · Unicode 5.1支持;
    · 升级了类加载器架构,包括了一个关闭URLClassLoader的方法;
    · 虚拟机增强:压缩64位对象指针,新的G1垃圾回收器,对非Java语言的支持(InvokeDynamic);
    · 为Java 2D提供的XRender管道。
    除了等待今年晚些时候的JDK 7官方发布,你也可以在其早期版本中尝试其中的一些特性,可以去http://java.sun.com/javase/downloads/ea.jsp下载JDK 7第5个里程碑版本(目前最新的版本)。
    本文将重点介绍语言新特性中的二进制字面量,在switch中使用字符串和整型字面量下划线,我的环境是Windows XP SP3 JDK 7里程碑5版本,本文引用的示例代码可从http://www.informit.com/content/images/art_friesen_exploringjdk1/elementLinks/code.zip打包下载。
    
     二进制字面量
    Java从C/C 继承了用十进制(63),十六进制(0x3f)和八进制符号表示整型字面量,JDK 7也允许你增加0B或0b前缀用二进制符号表示整型字面量,如:
    int x = 0b10101111;
    System.out.printf (
"%d%n", x); // Output: 175
    转换为二进制
    java.util.Formatter类中的System.out.printf()方法提供了格式转换功能,你可以使用它将一个整数转换成十进制,十六进制和八进制符号,但它(仍然)不支持转换成二进制,必须借助整数的toBinaryString()方法进行转换:
    System.out.printf ("%s%n", Integer.toBinaryString (78));
    这段代码将输出1001110,如果你希望Integer.toBinaryString()的输出结果包括首位的0(这在匹配列中二进制数字时非常有用),但不幸的是,这个方法不能满足你的愿望,必须再寻找另外的办法。
    你可能会疑惑为什么二进制字面量怎么会包含在JDK 7中,据这个特性的创始人Derek Foster讲,使用按位运算的代码更具可读性,更容易验证使用二进制数字指定常量的技术规范,他同时指出,从心理上讲,从二进制转换成十六进制容易犯错。
    当然,你也可以依赖整数的parseInt()方法将二进制数字字符串转换成整数,如Integer.parseInt ("00110011", 2)将返回51,但是,由于下列原因调用这个方法会有问题:
    · 这个方法调用比直接使用字面量更冗长,它的调用语法极其凌乱;
    · 这个方法调用会带来一定的性能损失;
    · 编译器不能内联这个方法调用返回的值,但可以内联一个常量的值;
    · 在字符串中检查到错误时,这个方法调用会抛出一个异常,我们在编译时才能捕获这个异常;
    · 与二进制字面量不一样,不能使用switch语句的选择器值表示一个方法调用,如case Integer.parseInt ("00001110", 2):这样的语法是不正确的(也很丑陋),而case 0B00001110:这样的语法就是正确的(也易于阅读)。
    在字符串上使用switch
    在JDK 7中,switch语句进行了小幅升级,现在可以在字符串上使用switch了,你可以给switch语句提供一个字符串表达式,也可以给每个case提供一个常量字符串表达式,清单1是一个使用这个特性的WC(字数统计)程序的代码。
    清单1 // WC.java
    
import java.io.IOException;
    
public class WC
    {
    
public static void main (String [] args) throws IOException
    {
    
boolean caseInsensitive = false;
    
boolean verbose = false;
    
for (String arg: args)
    
switch (arg)
    {
    
case "-i":
    
case "-I": caseInsensitive = true;
    
break;
    
    
case "-V":
    
case "-v": verbose = true;
    
break;
    
default  : System.err.println ("usage  : "
    
"java WC [-i|-I -v|-V] stdin");
    System.err.println (
"example: java WC -v <WC.java");
    
return;
    }
    
if (verbose)
    countWordsVerbose (caseInsensitive);
    
else
    countWords ();
    }
    
static void countWords () throws IOException
    {
    
int ch, nWords = 0;
    
while ((ch = System.in.read ()) != -1)
    {
    
if (Character.isLetter (ch)) // Start of word is indicated by letter.
    
         {
    
do
    {
    ch 
= System.in.read ();
    }
    
while (Character.isLetterOrDigit (ch));
    nWords
;
    }
    }
    System.out.println (
"\nTotal words = "   nWords);
    }
    
static void countWordsVerbose (boolean caseInsensitive) throws IOException
    {
    
int ch;
    WordNode root 
= null;
    
while ((ch = System.in.read ()) != -1)
    {
    
if (Character.isLetter (ch)) // Start of word is indicated by letter.
    
         {
    StringBuffer sb 
= new StringBuffer ();
    
do
    {
    sb.append ((
char) ch);
    ch 
= System.in.read ();
    }
    
while (Character.isLetterOrDigit (ch));
    
if (root == null)
    root 
= new WordNode (sb.toString ());
    
else
    root.insert (sb.toString (), caseInsensitive);
    }
    }
    display (root);
    }
    
static void display (WordNode root)
    {
    
// root == null when leaf node has been reached (or perhaps there are no
    
// words in tree)
    
      if (root == null)
    
return;
    
// Display all words lexicographically less than the word in the current
    
// node.
    
      display (root.left);
    
// Display current node's word and number of occurrences.
    
      System.out.println ("Word = "   root.word   ", Count = " 
    root.count);
    
// Display all words lexicographically greater than the word in the
    
// current node.
    
      display (root.right);
    }
    }
    
class WordNode
    {
    String word;    
// Stored word
    
   int count = 1;  // Number of occurrences of word in text
    
   WordNode left;  // Left subtree
    
   WordNode right; // Right subtree
    
   public WordNode (String word)
    {
    
this.word = word;
    left 
= right = null;
    }
    
public void insert (String word, boolean caseInsensitive)
    {
    
int order = (caseInsensitive) ? this.word.compareToIgnoreCase (word)
    : 
this.word.compareTo (word);
    
if (order > 0// word argument lexicographically less than current
    
// word
    
      {
    
// If left-most leaf node reached then insert new node as its
    
// left-most leaf node; otherwise, keep searching left.
    
          if (left == null)
    left 
= new WordNode (word);
    
else
    left.insert (word, caseInsensitive);
    }
    
else
    
if (order < 0// word argument lexicographically greater than current
    
// word
    
      {
    
// If right-most leaf node reached then insert new node as its
    
// right-most leaf node; otherwise, keep searching right.
    
          if (right == null)
    right 
= new WordNode (word);
    
else
    right.insert (word, caseInsensitive);
    }
    
else
    
this.count // Update number of found occurrences.
    
   }
    }

    上面的例子充分说明了处理命令行参数时在字符串上使用switch是很有用的,可以替代这个功能的是if-else if … else表达式,但这样一来会使代码更冗长。
    编译好WC.java后,指定(-i或I,区分大小写)和(-v或-V,输出详细信息)命令行参数运行这个程序,如:

    java WC <WC.java        // Count the number of words in WC.java and report the total.
    
java WC -<WC.java     // Count the number of occurrences of each word in WC.java and report 
    
// each total.
    
java WC --<WC.java  // Count the number of occurrences of each word in WC.java and report 
    
// each total. Use a case-insensitive comparison so that, for example, 
    
// this and This are treated as two occurrences of the same word instead 
    
// of one occurrence each of two different words.
    整型字面量下划线
    JDK 7支持数字下划线,改善了二进制,十进制,十六进制和八进制字面量的可读性,如:

    int mb_directory_info = 204_555_1212;
    System.out.printf (
"%d%n", mb_directory_info); // Output: 2045551212
    
long debt = 11_000_000_000_000L;
    System.out.printf (
"%d%n", debt); // Output: 11000000000000
    
byte max_pos_value = 0x0___07F;
    System.out.printf (
"%d%n", max_pos_value); // Output: 127
    你可以在连续数字之间插入一到多个下划线,但不能在数字的最前面指定下划线(如_25这样是不允许的),因为这样将被解释为一个标识符,同样,也不能用下划线作为后缀(如0x3f_这样也是不允许的)。
    虽然Foster提到Integer和Long的decode()方法将支持这个特性,但目前的版本还不支持,同样,Integer.parseInt()和Long.parseLong()也不支持这个特性。
    
小结
    二进制字面量,switch对字符串的支持和整型字面量下划线支持仅仅是JDK 7新语言特性的一小部分,可以说它们是小而强大,但与闭包和模块化比起来,很多人可能会觉得它们微不足道。下一篇文章将会介绍起源于Java SE 6的半透明和任意形状的窗口的改进。

    
 

                   

 

相关新闻

每一个程序员要遵守的一些优秀编程风格
第1篇 计算机的基础知识
代词it, that, one, the one 的辨析
《VB程序设计基础》选择题
Is Family Life Changing
设计模式之Iterator
设计模式之Visitor
设计模式之Interpreter(解释器)
设计模式之Mediator(中介者)
设计模式之Strategy(策略)

您可能对这些感兴趣  

用VB做定时断线程序
VisualBasic中的界面设计原则和编程技巧
VB6.0与Windows API 间的呼叫技巧
制作可以自动隐藏的弹出式菜单
ListBox中的字符串超长显示的解决方法
VB中的Unicode 和 Ansi 格式
优化程序显示速度
Visual Basic 产生渐层的 Form 背景
用VB实现客户——服务器(TCP/IP)
用VB制作注册软件的方法

题目筛选器
日期:
类型:
状态:
得分: <=
分类:
作者:
职业:
关键字:
搜索

 
 
 
  焦点事件
 
  知识体系
 
  职业列表
 
 
  最热文章
 
 
  最多引用文章
 
 
  最新文章
 
 
 
 
网站介绍 | 广告服务 | 招聘信息 | 保护隐私权 | 免责条款 | 法律顾问 | 意见反馈
版权所有 不得转载
沪ICP备 10203777 号 联系电话:021-54428255
  帮助提示    
《我的太学》是一种全新的应用,您在操作中遇到疑问或者问题,请拨打电话13564659895,15921448526。
《我的太学》