java7新特性

java7新特性

  • switch中使用String
  • try-with-resources
  • 捕捉多个异常
  • 泛型实例化类型自动推断
  • 增加二进制表示
  • 数字中可添加分隔符

switch中使用String

/**
 * 在java7以前,switch只能用于byte,char,int类型,java7开始就有可以用于string类型
 */
public class Switch {
    public static void main(String[] args) {
        String str = "testA";
        switch (str) {
            case "testA":
                System.out.println("testA");
                break;
            case "testB":
                System.out.println("testB");
                break;
            default:
                System.out.println("default");
                break;
        }

    }
}

try-with-resources

/**
 * 实现了java.lang.AutoCloseable接口或者java.io.Closeable接口(Closeable继承AutoCloseable)的对象,可以采用try-with-resources方式。
 * 采用try-with-resources方式后,不需要再次声明流的关闭.。
 * 例如 Reader,实现了Closeable接口。
 */
public class TrywithResource {
    public static void main(String[] args) {
        OutputStream fos = null;
        //java7之前,需要手动添加close方法,关闭流对象
        try {
            fos = new FileOutputStream("D:/text.txt");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        //java7开始,不需要手动添加close方法
        //AutoCloseable接口只有一个方法,close()
        //close方法是AutoCloseable接口的唯一方法,该方法在程序运行时会被自动调用。
        //如果在try-with-resources语句中遇到了异常,close关闭语句会先于catch语句执行。
        try(OutputStream fos1 = new FileOutputStream("D:/text.txt");){
            System.out.println("test test test");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        //假如try-with-resources中使用了非AutoCloseable的子类,那么会提示下方的提示
        //The resource type File does not implement java.lang.AutoCloseable
        //如下:
//        try(Switch s=new Switch()){
//            
//        }
    }
}

捕捉多个异常

/**
 * 从java7开始catch异常可以catch多个
 */
public class ExceptionCatch {
    public static void main(String[] args) {
        //java7之前捕捉多个异常需要多个catch
        try {
            throwAll();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        //java7开始可以catch多个异常
        try {
            throwAll();
        } catch (IOException | SQLException e) {
            e.printStackTrace();
        }
    }

    public static void throwAll() throws IOException, SQLException {
        ioThrow();
        sqlThrow();
    }

    public static void ioThrow() throws IOException {
        throw new IOException();
    }

    public static void sqlThrow() throws SQLException {
        throw  new SQLException();
    }
}

泛型实例化类型自动推断

/**
 * 泛型实例化类型自动推断
 */
public class Genericity {
    public static void main(String[] args) {
        //java7之前
        List<String> listBefore7=new ArrayList<String>();
        //java7之后
        List<String> listAfter7=new ArrayList<>();
    }
}

增加二进制表示

/**
 * 在java开始新增了二进制表示
 */
public class Binary {
    public static void main(String[] args) {

        //二进制
        int a = 0b0001_1001;
        //8进制,不能少了最前的那个0
        int b=0144;
        //十进制
        int c=123;
        //16进制
        int d=0x100F;

        System.out.println("二进制:"+a);
        System.out.println("8进制:"+b);
        System.out.println("十进制:"+c);
        System.out.println("16进制:"+d);
    }
}

数字中可添加分隔符

/**
 * Java7中支持在数字中间增加'_'作为分隔符,分隔长int以及long(也支持double,float),显示更直观,如(12_123_456)。
 * 下划线只能在数字中间,编译时编译器自动删除数字中的下划线。
 */
public class NumberSplit {
    public static void main(String[] args) {
        int intOne = 1_000_000;
        long longOne = 1_000_000;
        double doubleOne = 1_000_000;
        float floatOne = 1_000_000;

        System.out.println("intOne:"+intOne);
        System.out.println("longOne:"+longOne);
        System.out.println("doubleOne:"+doubleOne);
        System.out.println("floatOne:"+floatOne);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值