ITPUB论坛 » Java入门与认证版 » Java1.5语言新特性简单总结
新一届的微软MVP评选已经开始,欢迎各位推荐!
2008-4-23 00:40 rbible
Java1.5语言新特性简单总结

1. 自动装箱与拆箱 对应C#

例1.1
Integer i = 10;
int j = i;


2. 更优化的for循环 对应就C#---foreach循环

例2.1
String[] names = {"BadBoy","GoodBoy","HappyGirl","sadGirl"};
for(String option: names) {
    System.out.println(option);
}

例2.2 加泛型 对应C++模板
import [url=http://www.ttadd.com/diannao/List/List_379.html][size=2]Java[/size][/url].util.*;
ArrayList<String> animals = new ArrayList<String>();
animals.add("Dog");
animals.add("Cat");
animals.add("Chick");
animals.add("Cow");
for(String option : animals) {
    System.out.println(option);
}


3.参数可变的方法和printf

例3.1
定义:
public int sum(int... n) { //传过来n为一个int型数组
    int tempSum;
    for(int option : n) {
        tempSum+=option;
    }
/*
    for(int i = 0; i < n.length; i++) {
        tempSum+=n[i];
[/i]    }
*/
    return tempSum;
}
调用1: sum(1);
调用2: sum(1,2);
调用3: sum(1,2,3,4);

例3.2 printf方法, 对应c语言的printf
int x = 10;
int y = 20;
int sum = x + y;
System.out.printf("%d + %d = %d",x,y,sum);


4. 枚举

例4.1
public enum MyColors { red , black , blue , green , yellow }
MyColors color = MyColors.red;
for(MyColors option : color.values()) {
    System.out.println(option);
}
/**不能在switch语句里这样写case MyColors.red:
*这样编译器不会让你通过*/
switch(color) {
case red:  System.out.println("best color is "+red);  
                 break;
case black:  System.out.println("NO " + black);
                    break;
default:  System.out.println("What");
                break;
}


5.静态引用
例5.1
1.5版本以前的写法是:
import [url=http://www.ttadd.com/diannao/List/List_379.html][size=2]Java[/size][/url].lang.Math; //程序开头处
...
double x = Math.random();
1.5版本中可以这样写
import static [url=http://www.ttadd.com/diannao/List/List_379.html][size=2]Java[/size][/url].lang.Math.random; //程序开头处
...
double x = random();

[[i] 本帖最后由 rbible 于 2008-8-18 22:12 编辑 [/i]]

2008-4-23 07:12 justforregister
nice job

2008-4-23 08:55 zhour560
写的不错,简洁易懂!

2008-4-23 14:35 FormatMe
简单明了

2008-4-23 15:32 Narcissus丶
都是高人 - -!

2008-5-4 10:38 leimingliang
ok

2008-7-3 17:51 laobai1982
好玩意收藏了!

2008-7-5 06:32 sr0734
还有Annatation
@Override
@EJB

2008-7-5 10:38 Sky-Tiger
GOOD!

2008-7-7 16:53 alex0058
言简意赅,厉害

2008-7-7 17:35 it01
good

2008-7-7 20:21 VagrantLee
呵呵...
Nice Job!

2008-7-18 15:48 voca
这个,请问1.5 VS 1.4的总结有么?

呵呵

2008-7-18 21:59 justforregister
还不错

2008-8-6 12:59 levis2000
Annotations漏了

2008-8-6 20:37 VagrantLee
高手,收藏先!

2008-8-6 20:57 Sky-Tiger
其他呢?

ANNOTATION
泛型
新的并发技术

2008-8-18 22:18 rbible
回复 #17 Sky-Tiger 的帖子

总结这东西也是比较花时间的

全面的总结一遍是不可能的

欢迎“天王盖地虎”回帖,补充,追加...

我一定修改编辑进去...

[[i] 本帖最后由 rbible 于 2008-8-18 22:19 编辑 [/i]]

2008-8-18 22:42 Sky-Tiger
楼主到省事啊!好人还是做到底好!

页: [1]


Powered by ITPUB论坛