热门文章
阿标在线 动力3.62HTML生成3.62网站文件说明
动力3.62整合动网7.0 SP2插
MDAC2.8 下载!
动力3.62版 防止垃圾留言
动力3.6全方位改动方法
让3.62不同频道实现不同风
把3.62首页登陆为横向代码
动易3.6首页随机FLASH修改
362首页和文章频道页图文幻
个性化修改3.6宝典
3.62轻易实现网摘功能
如何正确统计中文字数?
弹出JAVASCRIPT语法错误对
后台使“网站顶部LOGO地址
最新图片文章横向移动的修
html 生成艺术字
3.6 Sp2 Logo和Banner及广
日期值的计算
汉字转拼音
首页“图片更新”图片滚动
简体中文转换为繁体中文的
如何在css中定义链接的下划
Java1.5语言新特性简单总结
[ 录入:阿标 | 点击数: | 更新时间:2005-3-18 12:44:00]
********************* 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 java.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];
}
*/
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 java.lang.Math; //程序开头处
...
double x = Math.random();
1.5版本中可以这样写
import static java.lang.Math.random; //程序开头处
...
double x = random();