• JDK1.5以后的版本特性


    一、JDK1.5新特性

      1.泛型:泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数。这种参数类型可以用在类、接口和方法的创建中,分别称为泛型类、泛型接口、泛型方法。可以在编译的时候就能发现一些参数类型导致的异常。

      2.自动拆箱和装箱:

        Java为我们提供了8中基本类型的数据,但是有时候我们需要传入的是对象如集合中加入数字,这就需要包装类型。所以Java为每种基本数据类型都提供了对应的包装器类型,Byte、Short、Integer、Long、Float、Double、Character、Boolean。

        所谓的拆箱就是自动从包装类型变为基本数据类型,装箱就是自动从基本数据类型转为包装类型。

        装箱过程是通过调用包装器的valueOf方法实现的,而拆箱过程是通过调用包装器的 xxxValue方法实现的。(xxx代表对应的基本数据类型)。如装箱Integer a = Integer.valueOf(1),拆箱int b = a.intValue();

        

    public static void main(String[] args) {
            Integer a = new Integer(127);
            Integer b = new Integer(127);
            int c = 127;
            Integer d = 127;
            Integer e = 127;
            Integer f = 128;
            Integer g = 128;
            int h =128;
            System.out.println((a == b) +"      "+ (a.equals(b)));//false      true
            System.out.println((a == c));                         //true
            System.out.println((a == d) +"      "+ (a.equals(d)));//false      true
            System.out.println((c == d));                         //true
            System.out.println((d == e) +"      "+ (d.equals(e)));//true      true
            System.out.println((f == g) +"      "+ (f.equals(g)));//false      true
            System.out.println((g == h));                         //true
            //Byte,Short,Integer,Long都是有默认缓存的-128到127的数字,所以在比较这个区间的数据的地址时,是相同的。
            //equals方法会将这些数据转换到基本类型然后再比较
            //包装类型和基本数据类型 == 比较时会先拆箱然后比较
            
            Character c1 = 'a';
            Character c2 = 'a';
            Character c3 = new Character('a');
            char c4 = 'a';
            System.out.println(c1 == c2);//true
            System.out.println(c1 == c3);//false
            System.out.println(c3 == c4);//true
            
            
        }

      3.枚举:可以说是一类有限事物的集合,将有限的事务放在一起,列举出来。

        

    public enum Season {
        
        SPRING("春"),
        SUMMER("夏"),
        AUTUMN("秋"),
        WINTER("冬");
    
        private String value;
        
        private Season(String value){
            this.value = value;
        }
    
        public String getValue() {
            return value;
        }
        
    }
        public static void main(String[] args) {
            Season spring = Season.SPRING;
            Season spring2 = Season.SPRING;
            Season summer = Season.SUMMER;
            System.out.println(spring == spring2);//true 单例的一种实现方法
            System.out.println(spring == summer);
            System.out.println(Season.SPRING.getValue());//
            
        }

      4.增强for循环(foreach)  

        public static void main(String[] args) {
           List<String> list = new ArrayList<String>();
           list.add("a");
           list.add("b");
           list.add("c");
           list.add("d");
           for(String item : list){
               System.out.println(item);
           }
        }

      5.静态导入 :要使用静态成员(方法和变量)我们必须给出提供这个静态成员的类,使用静态导入可以使被导入类的静态变量和静态方法在当前类直接可见,使用这些静态成员无需再给出他们的类名。

    public class StateCode {
    
        public static int normal = 1;
        public static int error = 2;
        
        public static void getState(){
            System.out.println("this is state code"); 
        }
    }
    public class Test {
        
        public static void main(String[] args) {
            System.out.println(normal);
            getState();
        }
    
    }

      6.内省 :通过反射来实现,主要是获取并操作对象的属性的getter和setter方法,达到在运行过程中操作对象的目的。其底层还是通过反射来实现的。

      

    package jdk5;
    
    import java.beans.BeanInfo;
    import java.beans.Introspector;
    import java.beans.MethodDescriptor;
    import java.beans.PropertyDescriptor;
    import java.lang.reflect.Method;
    
    public class Test {
        
        public static void main(String[] args) throws Exception {
            User user = new User();
            PropertyDescriptor propertyDescriptor =  new PropertyDescriptor("name",User.class);
            //获取属性的写的方法。
            Method writeMethod = propertyDescriptor.getWriteMethod();
            //获取属性的读方法。
            Method readMethod = propertyDescriptor.getReadMethod();
            propertyDescriptor.getReadMethod();
            writeMethod.invoke(user, "kyle");
            System.out.println(readMethod.invoke(user, null));
            
            BeanInfo beanInfo=Introspector.getBeanInfo(User.class);
            
            //获取所有的方法
            MethodDescriptor[] methodDescriptors = beanInfo.getMethodDescriptors();
            for(MethodDescriptor methodDescriptor : methodDescriptors){
                System.out.println(methodDescriptor.getName());
            }
            
            //获取所有的属性
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for(PropertyDescriptor pd : propertyDescriptors){
                System.out.println(pd.getName());
            }
        }
    
    }

      7.可变参数:可变参数其实就是一个数组

      

    public class Test {
        
        public static void main(String[] args) throws Exception {
            System.out.println(add(1,2,3));
            System.out.println(add(1,2,3,4));
        }
        
        public static int add(int a, int... b){
            int result = a;
            for(int temp : b){
                result += temp; 
            }
            return result;
        }
    
    }

      8.线程池

         Java5中,对Java线程的类库做了大量的扩展,其中线程池就是Java5的新特征之一,除了线程池之外,还有很多多线程相关的内容,为多线程的编程带来了极大便利。为了编写高效稳定可靠的多线程程序,线程部分的新增内容显得尤为重要。

    二、JDK1.6新特性

      1.Desktop类和SystemTray类 

      2.使用JAXB2来实现对象与XML之间的映射

      3.理解StAX 

      4.使用Compiler API 

      5.轻量级Http Server API 

      6.插入式注解处理API(Pluggable Annotation Processing API) 

      7.用Console开发控制台程序 

      8.对脚本语言的支持如: ruby, groovy, javascript. 

      9.Common Annotations 

    三、JDK1.7新特性

      1.switch中可以使用字串了

        

    public class Test {
        
        public static void main(String[] args) throws Exception {
            String season ="summer";
            String result = "";
            switch (season){
            case "spring":
                    result = "春";    
                    break;
            case "summer":
                result = "夏";    
                break;
            case "autumn":
                result = "秋";    
                break;
            case "winter":
                result = "冬";    
                break;
            default :
                result = "输入有误";
            }
            
            System.out.println(result);
        }
    }

      2.泛型实例化类型自动推断

      

    public class Test {
        
        public static void main(String[] args) throws Exception {
            List<String> list = new ArrayList<>();
            list.add("a");
            System.out.println(list.get(0));
        }
    }

      

      3.支持数字用下划线分隔提升可读性

    public static void main(String[] args) throws Exception {
           int num = 21_112_123;
           System.out.println(num);//21112123
        }

      

      4.支持二进制数字

    public static void main(String[] args) throws Exception {
           int num = 0b10001;
           System.out.println(num);//17
        }

      

      5.自动资源管理

        

    public class Test {
    
        public static void main(String[] args) {
            String src = "D:/logs/log.log";
            String ds = "D:/logs/log2.log";
            try (InputStream in = new FileInputStream(src); 
                 OutputStream out = new FileOutputStream(ds)) {
                byte[] buf = new byte[8192];
                int n;
                while ((n = in.read(buf)) >= 0)
                    out.write(buf, 0, n);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    四、JDK1.8特性

      1.接口的默认方法 :java8允许接口有一个默认的非抽象方法,所有实现该接口的类可以直接调用该方法。

    public interface AbstractInterface {
        
        default void sayHello() {
           System.out.println("hello");
        }
        
    }
    public class MyClass implements AbstractInterface{
    
        public static void main(String[] args) {
           new MyClass().sayHello();//hello
        }
    }

      2. Lambda 表达式

        

     public static void main(String[] args) {
            List<String> names = new ArrayList<>();
            names.add("kyle");
            names.add("stan");
            names.add("eric");
            names.add("kenny");
            
            System.out.println(names);//[kyle, stan, eric, kenny]
            
            //Lambda 表达式
            Collections.sort(names, (String a, String b) -> {
                return b.compareTo(a);
            });
            
            System.out.println(names);//[stan, kyle, kenny, eric]
            
        }

      3.函数式接口

      4.Date API

      5.多重注解

  • 相关阅读:
    介绍下自己的Delphi学习环境
    我所理解的Delphi中的数组类型
    字符串的基本操作
    以太网网络变压器的作用
    S3C2416 2D加速
    DM9000AEP调试的时候注意事项
    设置activity背景图片
    如何從現有的share library開發!?
    struct mntent linux挂载信息读取
    Qt中Qstring,char,int,QByteArray之间到转换
  • 原文地址:https://www.cnblogs.com/kyleinjava/p/9626789.html
Copyright © 2020-2023  润新知