JAVA1.5新特性

今天看代码发现有很多看不懂的写法,问下张海才知道是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 ,能力,断言==
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值