今天看代码发现有很多看不懂的写法,问下张海才知道是JAVA1.5的新特性,于是上网找了下,了解到1.5的新特性:
1.泛型. 新引进的重头,有点像C++的模板.
泛型是方便对集合的操作,省去复杂的类型转换.
import java.util.*;
ArrayList<string></string> animals = new ArrayList<string></string>();
animals.add("Dog");
animals.add("Cat");
animals.add("Chick");
animals.add("Cow");
for(String option : animals) {
System.out.println(option);
}
2.自动拆箱和装箱
也是为了省去复杂的类型转换,还有引进了像printf的匹配
Integer i = 10;
int j = i;
int x = 10;
int y = 20;
int sum = x + y;
System.out.printf("%d + %d = %d",x,y,sum);
3.for循环 对应C#的foreach
相当于域的概念.
String[] names = {"BadBoy","GoodBoy","HappyGirl","sadGirl"};
for(String option: names) {
System.out.println(option);
}
4.可变参数
参数可变化的,可用type...来书写
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];
}
*/
return tempSum;
}
调用1: sum(1);
调用2: sum(1,2,0);
调用3: sum(1,2,3,4,9);
5.枚举类型
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;
}
6.静态引入
不用导入整个,可以导入静态的方法,字段
import static java.lang.Math.random; //程序开头处
...
double x = random();
7.标注
@override等方便测试的特性
8. Callable 和 Future ,能力,断言==
1.泛型. 新引进的重头,有点像C++的模板.
泛型是方便对集合的操作,省去复杂的类型转换.
import java.util.*;
ArrayList<string></string> animals = new ArrayList<string></string>();
animals.add("Dog");
animals.add("Cat");
animals.add("Chick");
animals.add("Cow");
for(String option : animals) {
System.out.println(option);
}
2.自动拆箱和装箱
也是为了省去复杂的类型转换,还有引进了像printf的匹配
Integer i = 10;
int j = i;
int x = 10;
int y = 20;
int sum = x + y;
System.out.printf("%d + %d = %d",x,y,sum);
3.for循环 对应C#的foreach
相当于域的概念.
String[] names = {"BadBoy","GoodBoy","HappyGirl","sadGirl"};
for(String option: names) {
System.out.println(option);
}
4.可变参数
参数可变化的,可用type...来书写
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];
}
*/
return tempSum;
}
调用1: sum(1);
调用2: sum(1,2,0);
调用3: sum(1,2,3,4,9);
5.枚举类型
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;
}
6.静态引入
不用导入整个,可以导入静态的方法,字段
import static java.lang.Math.random; //程序开头处
...
double x = random();
7.标注
@override等方便测试的特性
8. Callable 和 Future ,能力,断言==