正 文

JDK5.0的11个主要新特征


www.7dspace.com  更新日期:2006-2-23 6:57:28  七度空间


7  元数据(Meta data)

  请参考

  http://www-900.ibm.com/developerWorks/cn/java/j-annotate1/

  http://www-900.ibm.com/developerworks/cn/java/j-annotate2.shtml

8 Building Strings(StringBuilder类)

   在JDK5.0中引入了StringBuilder类,该类的方法不是同步(synchronized)的,这使得它比StringBuffer更加轻量级和有效。

9 控制台输入(Console Input)

  在JDK5.0之前我们只能通过JOptionPane.showInputDialog进行输入,但在5.0中我们可以通过类Scanner在控制台进行输入操作

   例如在1.4中的输入

String input = JOptionPane.showInputDialog(prompt);

int n = Integer.parseInt(input);
double x = Double.parseDouble(input);
s = input;

  在5.0中我们可以

Scanner in = new Scanner(System.in);

System.out.print(prompt);
int n = in.nextInt();
double x = in.nextDouble();
String s = in.nextLine();

10      Covariant Return Types(不晓得怎么翻译,大概是 改变返回类型)

  JDK5之前我们覆盖一个方法时我们无法改变被方法的返回类型,但在JDK5中我们可以改变它

  例如1.4中我们只能

public Object clone() { ... }

...
Employee cloned = (Employee) e.clone();

  但是在5.0中我们可以改变返回类型为Employee

public Employee clone() { ... }

...
Employee cloned = e.clone();

11 格式化I/O(Formatted I/O)

  增加了类似C的格式化输入输出,简单的例子:

public class TestFormat{

    public static void main(String[] args){
        int a = 150000, b = 10;
        float c = 5.0101f, d = 3.14f;
        System.out.printf("M M%n", a, b);
        System.out.printf("%x %x%n", a, b);
        System.out.printf("%3.2f %1.1f%n", c, d);
        System.out.printf("%1.3e %1.3e%n", c, d*100);
    }
}

  输出结果为:

150000   10

249f0 a

5.01 3.1

5.010e+00 3.140e+02

6页,页码:[1] [2] [3] [4] [5] [6] 

上一篇:强悍的社区构建工具Community新版发布
下一篇:轻松编写自己的拖拉机算法
JDK5.0的11个主要新特征 作者:CSDN 来源:kmlzkma
收藏此页】【打印】【关闭
站 内 搜 索
 

热 点 导 读
特 别 推 荐