栏目导航
热点推荐
- 三十条有用的 Java 编程规则
- Java制作水印图片源码
- Java常见异常及可能的导致原因
- Java中的修饰词使用方法总结
- J2EE系统异常的处理准则
- Java中的异常、断言、日志解析(
- Java面试技巧:Java面试题集锦(
- 面向Java开发人员的Scala指南:
- Java程序员:一刻钟精通正则表达
- 网友经验分享:学好java开发的关
- 专家解答:创建表格与数据库进行
- Java远程访问Domino数据库
阅览排行
探秘JDK 7:将会出现新的语言特性
www.jz123.cn 2010-06-30 来源: 我要投递新闻
在字符串上使用switch
在JDK 7中,switch语句进行了小幅升级,现在可以在字符串上使用switch了,你可以给switch语句提供一个字符串表达式,也可以给每个case提供一个常量字符串表达式,清单1是一个使用这个特性的WC(字数统计)程序的代码。
清单1 WC.java
// 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
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
整型字面量下划线
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的半透明和任意形状的窗口的改进。
上一篇:Java无法编写?通过JNI本地编写来解决 下一篇:用JDOM完成Java更新XML文件的方法