Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区 » 学习心得/方法/资源  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Java1.5语言新特性简单总结
java_china





发贴: 7
积分: 0
于 2005-08-29 14:49 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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();

希望各位有新的发现,可以跟进,多谢!



作者 Re:Java1.5语言新特性简单总结 [Re:java_china]
hunterx





发贴: 1
积分: 0
于 2005-08-29 15:10 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
学习中,具体使用还要等一久。


作者 Re:Java1.5语言新特性简单总结 [Re:java_china]
justforfun





发贴: 11
积分: 0
于 2005-08-29 20:20 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
java_china wrote:
希望各位有新的发现,可以跟进,多谢!


You are a Thief!!

Copied from
http://www.javaresearch.org/article/showarticle.jsp?column=545&thread=32349



作者 Re:Java1.5语言新特性简单总结 [Re:java_china]
uefs





发贴: 1
积分: 0
于 2008-01-30 14:46 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
这些简单的总结真得是没有用处,到处都是简单总结,好象java真得很简单一样


作者 Re:Java1.5语言新特性简单总结 [Re:java_china]
JiafanZhou



版主


发贴: 736
积分: 61
于 2008-01-30 16:56 user profilesend a private message to usersend email to JiafanZhousearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
I like it, always like the KISS law. Keep it simple and stupid.


When I was a kid I used to pray every night for a new bike. Then I realized that The Lord doesn't work that way, so I stole one and asked him to forgive me.
作者 Re:Java1.5语言新特性简单总结 [Re:java_china]
flytiger





发贴: 79
积分: 21
于 2008-02-02 08:32 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list

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


correction
4. 枚举
例4.1
public enum MyColors {
red,
black,
blue,
green,
yellow
}

MyColors color = MyColors.red;
for(MyColors option : MyColors.values()) {
System.out.println(option);
}

/**不能在switch语句里这样写case MyColors.red:
*这样编译器不会让你通过*/
switch(color) {
case red:
System.out.println("best color is "+MyColors.red);
break;
case black:
System.out.println("NO " + MyColors.black);
break;
default:
System.out.println("What");
break;
}




public class HelloWorld {
public static void main(String[] argc) {
System.out.println("Hello World!");
}
}


the only code without bug is "Hello World"

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923