jdk1.5新特性

1.5jdk常用新特性:
1:AutoBoxing/AutoUnBoxing(自动装箱/拆箱)
2:Enum(枚举)
3:For-Each(增强for循环)
4:Varargs(可变长参数)

1:AutoBoxing&AutoUnBoxing.此特性是将基本类型/对象自动转换为对象/基本类型.
package newcharacter;
import java.util.HashMap;


public class AutoBoxingUnBoxing {
    public static void main(String[] args){
        HashMap map = new HashMap();
        map.put("A", new Integer(1));
        map.put("B", new Integer(2));
        map.put("C", 3);//java1.5特性,将基本的类型自动转换为对象.AutoBoxing(自动打包)
        map.put("D", 4);
        int temp = (Integer)map.get("A");//java1.5特性,通过强制转换,自动将对象转换为基础类型.AutoUnBoxing(自动解包)
        System.out.println(temp);
        System.out.println(map.get("C").equals(3));//java1.5特性,将基本的类型自动转换为对象.AutoBoxing(自动打包)       
    }
}

2:枚举:属性的调用.
public class EnumDemo {
    private enum Name{
        Daniel,Tiger,Jason
    }
    public static void main(String[] args){
        readName(Name.Daniel);
    }
    private static void readName(Name n){
        switch(n){
            case Daniel:
                System.out.println("My name is :"+Name.Daniel);
                break;
            case Tiger:
                System.out.println("My name is :"+Name.Tiger);
                break;
            case Jason:
                System.out.println("My name is :"+Name.Jason);
                break;
            default:
                System.out.println("No name");
                
        }
    }     
}

2:枚举的构造函数
package newcharacter;
public class EnumConstructor {
    public static void main(String[] args){
        for(Ages a:Ages.values()){
            System.out.println(a+" age is :"+a.age);
        }
    }
    public enum Ages{
        
        Daniel(24),Tiger(23),Jason(22);
        private final int age;
        Ages(int age){
            this.age = age;
        }
      
        public int getage(){
            return age;
        }
    }
}

3:增强For循环(For-Each)
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class StrongLoop {
public static void main(String[] args){
        int[]   ar={1,2,3,4,5};
        int sum=0;
        /*
         for (int i = 0; i < arr.length; i++) { 
            type var = arr[i];
            body-of-loop
          } 
         */ 
        for(int i =0 ; i<ar.length;i++){
            sum=+ar[i];
        }
        System.out.println("old loop for array:"+sum);
        /*
            for (type var : arr) {
            body-of-loop
            } 
         */ 
        for(int d:ar){
            sum=+d;
        }
        System.out.println("old loop for array:"+sum);
    
        List<String> list = new ArrayList();
        list.add("A");
        list.add("B");
        list.add("C");
        String oldloop="";
        String newloop="";
        /*
            for (Iterator<type> iter = coll.iterator(); iter.hasNext(); ) {
                type var = iter.next();
                body-of-loop
            } 
         */ 
        for(Iterator<String> it =list.iterator();it.hasNext(); ){
            String temp = it.next();
            oldloop+=temp;
        }
        System.out.println("old loop for Iterator:"+oldloop);
        /*
         for (type var : coll) {
            body-of-loop
         }
         */ 
        for(String t:list){
            newloop+=t;
        }
        System.out.println("new loop for Iterator"+newloop);
}
}

4:Varargs机制,定义实参个数可变的方法,形参的“类型”与“参数名”加三个“.”,private static void testOverload(int ... v);
public class OverLoadSimple {
    public static void main(String[] args){
        testOverload("result1",1);
        testOverload("result2",1,2);
         testOverload("result3",1,2,3);
    }
    private static void testOverload(String r,int n){
        int count;
        count=n;
        System.out.println(r+" is :"+count);
    }
    private static void testOverload(String r,int n, int m){
        int count;
        count = n+m;
        System.out.println( r+" is :"+count);
    }
    private static void testOverload(String r,int ... v){
        int count=0;
        for(int i :v){
            count=count+i;
        }
        System.out.println(r+" is :"+count);
    }

你可能感兴趣的:(java,C++,c,C#)