请先看以下两段代码(分别选自 Core Java 5E 和 Core Java 7E),不同之处已用蓝色表明。
程序一:
public class Welcome
{
public static void main(String[] args)
{
String[] greeting = new String[3];
greeting[0] = "Welcome to Core Java";
greeting[1] = "by Cay Horstmann";
greeting[2] = "and Gary Cornell";
for (int i = 0; i < greeting.length; i++)
System.out.println(greeting[i]);
}
}
程序二:
public class Welcome
{
public static void main(String[] args)
{
String[] greeting = new String[3];
greeting[0] = "Welcome to Core Java";
greeting[1] = "by Cay Horstmann";
greeting[2] = "and Gary Cornell";
for (String g : greeting)
System.out.println(g);
}
}
这两个程序输出的结果是相同的,均为:
Welcome to Core Java
by Cay Horstmann
and Gary Cornell
而程序二如果使用JDK1.4或以下版本编译就会出错,使用1.5则可顺利通过。这说明了此“:”操作符是JDK1.5新加的特性之一。
本文地址:http://com.8s8s.com/it/it11870.htm