网管联盟 | 网管论坛 | 网管u家 | 网管博客 | 网管软件 | 网管求职 | 小游戏 | 网管搜索 | 网管原创 | 网管聚合 | 网管读摘 | 网管焦点 | 世界素材 | 会员投稿 | 会员中心 
中国网管联盟
Windows Linux Cisco 网络技术 数据库 黑客攻防 DotNet Java PHP 认证 新闻资讯 服务器 存储资讯 网络设备 网管学堂 技术专题 焦点 网吧频道
 当前位置: > bitsCN.com > JAVA > 新手入门 > JDK > JDK核心API:Java1.5语言新特性简单总结  

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

2008-01-18  作者:bitsCN整理  来源:中国网管联盟  点评 投稿 收藏

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

  例1.1  

Integer i = 10;
  int j = i;
  2. 更优化的for循环 对应就C#---foreach循环 中国网管联盟bitsCN.com

  例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");

网管联盟bitsCN@com


  animals.add("Cow");
  for(String option : animals) {
  System.out.println(option);
  }
  3.参数可变的方法和printf 网管联盟bitsCN_com

  例3.1

网管u家u.bitsCN.com

  定义:  

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);

中国网管论坛bbs.bitsCN.com

  调用2: sum(1,2);

网管论坛bbs_bitsCN_com

  调用3: sum(1,2,3,4);

网管联盟bitsCN_com

  例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. 枚举
网管u家u.bitscn@com

  例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: 网管联盟bitsCN@com

  *这样编译器不会让你通过*/  

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.静态引用 网管bitscn_com

  例5.1 中国网管联盟bitsCN.com

  1.5版本以前的写法是:  

import java.lang.Math; //程序开头处
  ...
  double x = Math.random();
  1.5版本中可以这样写 网管联盟bitsCN@com

  import static java.lang.Math.random; //程序开头处 网管网www.bitscn.com

  ... 网管网www.bitscn.com

  double x = random();

网管bitscn_com


TAGs
 上一篇:用JDring包设置类似于cron的日程提醒   下一篇:基于JDK5.0的一些collection类使用总结
JDK核心API:Java1.5语言新特性简单总结 评论:
loading.. 评论加载中…
评论:请自觉遵守互联网相关政策法规,评论不得超过250字。

验证码: 注册用户
本类热门排行:
最新推荐文章:
网管论坛交流: