CTOCIO IT专家网

天极传媒 比特网 | 天极网 | IT专家网 | IT商网 | 52PK游戏网 | 手机天极 | IT分众 |
IT专家网搜索

您现在的位置: IT专家网 > Web服务子站 > 技巧

JDK核心API:Java1.5语言新特性简单总结

作者: loveofgod,  出处:赛迪网, 责任编辑: 李春禹, 
2008-01-15 14:07
  加泛型 对应C++模板  import java.util.*;定义:  public int sum(int...}  /**不能在switch语句里这样写case MyColors.red:*这样编译器不会让你通过*/  

  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 java.util.*;
  ArrayList animals = new ArrayList();
  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;
  }
  */
  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:

共2页。 1 2 :

网友评论

笔名 
请您注意:遵守国家有关法律、法规,尊重网上道德,承担一切因您的行为而直接或间接引起的法律责任。    IT专家网友拥有管理笔名和留言的一切权利。
  • 周排行榜
  • 月排行榜

邮件订阅

       
天极服务 | 关于我们 | 网站律师 | 加入我们 | 联系我们 | 广告业务 | 友情链接 | 我要挑错
All Rights Reserved, Copyright 2004-2008, Ctocio.com.cn
渝ICP证B2-20030003号 如有意见请与我们联系 powered by 天极内容管理平台CMS4i