008-jdk1.7版本新特性

一、JDK1.7

名称:Dolphin(海豚)

发布日期:2011-07-28

新特性:

1.1、switch-case中可以使用字串

  区分大小写。Java编译器通过switch使用String对象的 if-then-else 语句比链式语句生成通常更高效的字节码。

1.2、类型推断

  运用List<String> tempList = new ArrayList<>(); 即泛型实例化类型自动推断 

1.3、自动关闭类【自动释放类】

以下是jdk7 api中的接口,接口原型

/**
 * A resource that must be closed when it is no longer needed.
 *
 * @author Josh Bloch
 * @since 1.7
 */
public interface AutoCloseable {
    /**
     * Closes this resource, relinquishing any underlying resources.
     * This method is invoked automatically on objects managed by the
     * {@code try}-with-resources statement.
     *
     */
    void close() throws Exception;
}

自定义使用:

1.实现接口

2.使用try-catch代码块

示例

public class TryClose implements AutoCloseable {
    @Override
    public void close() throws Exception {
        System.out.println(" Custom close method … close resources ");
    }
}

使用

        try(TryClose tryClose =new TryClose()){

        }catch (Exception e){
            e.printStackTrace();
        }

参看:

//jdk自带类BufferedReader如何实现close方法(还有很多类似类型的类)

  public void close() throws IOException {
        synchronized (lock) {
            if (in == null)
                return;
            in.close();
            in = null;
            cb = null;
        }
    }

1.4、新增一些取环境信息的工具方法

File System.getJavaIoTempDir() // IO临时文件夹

File System.getJavaHomeDir() // JRE的安装目录

File System.getUserHomeDir() // 当前用户目录

File System.getUserDir() // 启动java进程时所在的目录

.......

1.5、Boolean类型反转,空指针安全,参与位运算

Boolean Booleans.negate(Boolean booleanObj)
True => False , False => True, Null => Null
boolean Booleans.and(boolean[] array)
boolean Booleans.or(boolean[] array)
boolean Booleans.xor(boolean[] array)
boolean Booleans.and(Boolean[] array)
boolean Booleans.or(Boolean[] array)
boolean Booleans.xor(Boolean[] array)

1.6、两个char间的equals

boolean Character.equalsIgnoreCase(char ch1, char ch2)

1.7、安全的加减乘除

int Math.safeToInt(long value)

int Math.safeNegate(int value)

long Math.safeSubtract(long value1, int value2)

long Math.safeSubtract(long value1, long value2)

int Math.safeMultiply(int value1, int value2)

long Math.safeMultiply(long value1, int value2)

long Math.safeMultiply(long value1, long value2)

long Math.safeNegate(long value)

int Math.safeAdd(int value1, int value2)

long Math.safeAdd(long value1, int value2)

long Math.safeAdd(long value1, long value2)

int Math.safeSubtract(int value1, int value2)

1.8、在try catch异常扑捉中,一个catch可以写多个异常类型,用"|"隔开

jdk1.7之前

try {
   ......
} catch(ClassNotFoundException ex) {
   ex.printStackTrace();
} catch(SQLException ex) {
   ex.printStackTrace();
}

jdk1.7之后

try {
   ......
} catch(ClassNotFoundException|SQLException ex) {
   ex.printStackTrace();
}

1.9、数值可加下划线

  例如:int one_million = 1_000_000;

1.10、支持二进制文字

  例如:int binary = 0b1001_1001;

1.11、简化了可变参数方法的调用  

  当程序员试图使用一个不可具体化的可变参数并调用一个*varargs* (可变)方法时,编辑器会生成一个“非安全操作”的警告。

1.12、对Java集合(Collections)的增强支持

在JDK1.7之前的版本中,Java集合容器中存取元素的形式如下:

以List、Set、Map集合容器为例: 

    //创建List接口对象
    List<String> list=new ArrayList<String>();
    list.add("item"); //用add()方法获取对象
    String Item=list.get(0); //用get()方法获取对象 

    //创建Set接口对象
    Set<String> set=new HashSet<String>();
    set.add("item"); //用add()方法添加对象 

    //创建Map接口对象
    Map<String,Integer> map=new HashMap<String,Integer>();
    map.put("key",1); //用put()方法添加对象
    int value=map.get("key"); 

在JDK1.7中,摒弃了Java集合接口的实现类,如:ArrayList、HashSet和HashMap。而是直接采用[]、{}的形式存入对象,采用[]的形式按照索引、键值来获取集合中的对象,如下:

       List<String> list=["item"]; //向List集合中添加元素

      String item=list[0]; //从List集合中获取元素

       Set<String> set={"item"}; //向Set集合对象中添加元素

       Map<String,Integer> map={"key":1}; //向Map集合中添加对象

      int value=map["key"]; //从Map集合中获取对象

猜你喜欢

转载自www.cnblogs.com/bjlhx/p/9705293.html