• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Java1.5语言新特性简单总结

servlet/jsp 搞代码 7年前 (2018-06-18) 131次浏览 已收录 0个评论

1. 自动装箱与拆箱 对应c#
 例1.1
  Integer i = 10;
  int j = i;
  
2. 更优化的for循环 对应就C#—foreach循环

http://www.gaodaima.com/40330.htmljava1.5语言新特性简单总结

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

欢迎大家阅读《Java1.5语言新特性简单总结》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Java1.5语言新特性简单总结
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址