Java5 新特性

Java 5 语言新特性体验
[ 2006-04-06 22:28:38 | 作者: yuhen ]
1. Boxing & UnBoxing

在1.4及以前的版本,Java并不支持自动装箱。如下面的例子。

Java 1.4
public class program {

  public static void main(String[] args) {
    
    // 1.
    int i = 13;
    Integer o = new Integer(i);
    
    // 2.
    Integer x = new Integer(13);
    int y = x.intValue();
  }
}

在 Java 5 中,自动装箱和拆箱的支持,使得我们可以像 C# 一样方便使用了。
Java 5
public class program {

  public static void main(String[] args) {
    
    // Boxing
    int i = 13;
    Integer o = i;
    
    // UnBoxing
    Integer x = new Integer(13);
    int y = x;
  }
}

不过 Java 5对于Boxing和UnBoxing仅仅是语言层面的封装,并没有得到JVM的支持,因此性能上要比C#差一些。

2. 可变参数(Vararg)
public class program {

  static void test(int... ints){
    for (int i = 0; i < ints.length; i++){
      System.out.println(ints[i]);
    }
  }
  
  public static void main(String[] args) {
    test(10, 20, 30, 40, 50);
  }
}

虽然看上去语法有点奇怪,不过翻译成C#就明白了。
public class program {

  static void Test(params int[] ints){
    for (int i = 0; i < ints.Length; i++){
      Console.WriteLine(ints[i]);
    }
  }
  
  static void Main(string[] args) {
    Test(10, 20, 30, 40, 50);
  }
}

3. For-Each

还是上面那个例子,我们改一下。
public class program {

  static void test(int... ints){
    for (int i : ints){
      System.out.println(i);
    }
  }
  
  public static void main(String[] args) {
    test(10, 20, 30, 40, 50);
  }
}

这个foreach语法好像比C#要简洁几个字母。 [smile]
public class program {

  static void Test(params int[] ints){
    foreach (int i in ints){
      Console.WriteLine(i);
    }
  }
  
  static void Main(string[] args) {
    Test(10, 20, 30, 40, 50);
  }
}

4. Enum

Java 5总算支持枚举了,这么常见的应用,真不知道Javaer这么多年是怎么过来的。
public class program {

  enum Color {
    Red,
    White,
    Yellow
  }
  
  public static void main(String[] args) {
    Color[] cs = Color.values();
    for(Color c : cs){
      System.out.println(c);
    }
  }
}

不过Java的枚举远比C#复杂,C# 的枚举是整数集合,而Java的枚举则是一个标准的类(class),因此支持方法、构造等等。(真的有必要吗? [confused] )
enum Color {
  Red,
  White ,
  Yellow;
  
  public void test() {}
}

在switch中的写法和C#也不大相同,在case语句中不能添加枚举名称。
public class program {

  enum Color {
    Red,
    White ,
    Yellow;
  }
  
  public static void main(String[] args) {
    Color c = Color.Red;
    switch (c){
      case Red: System.out.println("Red");break;
      default: System.out.println("Others"); break;
    }
  }
}

5. Static Import

使用静态导入可以使被导入类的所有静态变量和静态方法在当前类直接可见,使用这些静态成员无需再给出他们的类名。好似以前在C/C++/Delphi中的全局函数,晕~~~~ 这个东西还是尽可能不要用。
public class program {

  public static void main(String[] args) {
    int i = (int)(Math.random()*10);
    System.out.println(i);
  }
}

一旦使用Static Import,上面的代码就可以写成下面这个样子。
import static java.lang.Math.*;

public class program {

  public static void main(String[] args) {
    int i = (int)(random()*10);
    System.out.println(i);
  }
}

还是那句话,尽可能不要用这个。

6. 格式化输入输出

说实话,习惯了C# 的格式化输入输出(Console.WrtieLine / String.Format),再用 Java 的 System.out.println 还真不大习惯。现在好了,看看下面的代码,很像吧。
public class program {

  public static void main(String[] args) {
    System.out.printf("Integer=%d String=%s", 13, "abc");
  }
}

格式化的格式是 "%[argument_index$][flags][width][.precision]conversion"。

其中参数序号从1开始。如: System.out.printf("String=%2$s Integer=%1$d", 13, "abc");
详情请查看JDK5帮助文档中有关"Format String Syntax" 或者 "java.lang.System.out.printf"的说明。

----------------
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值