jdk7常用新特性总结



import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version $Id: practicejdk7.java, v 0.1 2018032715:16  Exp $
 */
public class practicejdk7 {
    public static void main(String[] args) throws IOException {
        /**
         *  可以用二进制来表示整数(byte,short,intlong)。使用二进制字面量的好处是,可以是代码更容易被理解。
         *  语法非常简单,只要在二进制数值前面加 0b或者0B
         */
        byte nByte = (byte)0b0001;
        short nShort = (short)0B0010;
        int nInt = 0b0011;
        long nLong = 0b0100L;

        /**
         * 对于一些比较大的数字,我们定义起来总是不方面,经常缺少或者增加位数。JDK7为我们提供了一种解决方案,下划线可以出现在数字字面量
         * 数字的开头或者结尾小数点的前后‘F’或者‘f’的后缀不能出现下划线
         */
        int a=10_000_000;
        long b = 0xffff_ffff_ffff_ffffl;
        System.out.println(a+","+b);

        /**
         * switch 语句可以用字符串了
         */
        String str="as";
        switch (str){
            case "a":
                System.out.println("why you are stupid");
            case "ass":
                System.out.println("you are correct");
            default:
                System.out.println("nothing");
        }

        /**
         * 泛型实例的创建可以通过类型推断来简化,以后你创建一个泛型实例,不需要再详细说明类型,只需用<>,编译器会自动帮你匹配
         */
        Map<String,List<String>> map=new HashMap<>();
        map=new HashMap<String,List<String>>();

        /**
         * Catch多个Exceptionrethrow exception 改进了类型检测
         */
        FileInputStream inputStream=null;
        FileOutputStream fileOutputStream=null;
        try{
            inputStream=new FileInputStream("D:/迅雷下载/图标2.jpg");
            fileOutputStream=new FileOutputStream(new File("D:/迅雷下载/张超红.jpg"));
            byte array[]= new byte[1024];
            int len=0;
            while ((len=inputStream.read(array))!=-1){
//                outputStream.write(array,0,len);
                fileOutputStream.write(array,0,len);
            }
        }catch (IOException |NullPointerException exception){

        }finally {
            if(inputStream!=null){
                inputStream.close();
            }
            if(fileOutputStream!=null){
                fileOutputStream.close();
            }
        }

    }
}

猜你喜欢

转载自blog.csdn.net/weixin_38907570/article/details/79716003