java 1.7 特性_jdk1.7新特性

1,switch中可以使用字串了

String s = "test";switch(s) {case "test":

System.out.println("test");case "test1":

System.out.println("test1");break;default:

System.out.println("break");break;

}

2,"<>"这个玩意儿的运用List tempList = new ArrayList<>(); 即泛型实例化类型自动推断。

import java.util.*;public classJDK7GenericTest {public static voidmain(String[] args) {//Pre-JDK 7

List lst1 = new ArrayList();//JDK 7 supports limited type inference for generic instance creation

List lst2 = new ArrayList<>();

lst1.add("Mon");

lst1.add("Tue");

lst2.add("Wed");

lst2.add("Thu");for(String item: lst1) {

System.out.println(item);

}for(String item: lst2) {

System.out.println(item);

}

}

}

3. 自定义自动关闭类

以下是jdk7 api中的接口,(不过注释太长,删掉了close()方法的一部分注释)

/*** A resource that must be closed when it is no longer needed.

*

*@authorJosh Bloch

*@since1.7*/

public interfaceAutoCloseable {/*** Closes this resource, relinquishing any underlying resources.

* This method is invoked automatically on objects managed by the

* {@codetry}-with-resources statement.

**/

void close() throwsException;

}

只要实现该接口,在该类对象销毁时自动调用close方法,你可以在close方法关闭你想关闭的资源,例子如下

class TryClose implementsAutoCloseable {

@Overridepublic void close() throwException {

System.out.println(" Custom close method …

close resources ");

}

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

public void close() throwsIOException {synchronized(lock) {if (in == null)return;

in.close();

in= null;

cb= null;

}

}

4. 新增一些取环境信息的工具方法

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

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

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

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

.......

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

Boolean Booleans.negate(Boolean booleanObj)

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

6. 两个char间的equals

boolean Character.equalsIgnoreCase(char ch1, char ch2)

7,安全的加减乘除

int Math.safeToInt(longvalue)int Math.safeNegate(intvalue)long Math.safeSubtract(long value1, intvalue2)long Math.safeSubtract(long value1, longvalue2)int Math.safeMultiply(int value1, intvalue2)long Math.safeMultiply(long value1, intvalue2)long Math.safeMultiply(long value1, longvalue2)long Math.safeNegate(longvalue)int Math.safeAdd(int value1, intvalue2)long Math.safeAdd(long value1, intvalue2)long Math.safeAdd(long value1, longvalue2)int Math.safeSubtract(int value1, int value2)

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

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

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

//创建List接口对象

List list=new ArrayList();

list.add("item"); //用add()方法获取对象

String Item=list.get(0); //用get()方法获取对象//创建Set接口对象

Set set=new HashSet();

set.add("item"); //用add()方法添加对象//创建Map接口对象

Map map=new HashMap();

map.put("key",1); //用put()方法添加对象

int value=map.get("key");

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

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

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

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

Map map={"key":1}; //向Map集合中添加对象

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

3.数值可加下划线

例如:

int one_million = 1_000_000;

4.支持二进制文字

例如:

int binary = 0b1001_1001;

5.简化了可变参数方法的调用

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

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

jdk7之前:

try{

......

}catch(ClassNotFoundException ex) {

ex.printStackTrace();

}catch(SQLException ex) {

ex.printStackTrace();

}

jdk7例子如下

try{

......

}catch(ClassNotFoundException|SQLException ex) {

ex.printStackTrace();

}

7、jdk7之前,你必须用try{}finally{}在try()内使用资源,在finally中关闭资源,不管try中的代码是否正常退出或者异常退出。jdk7之后,你可以不必要写finally语句来关闭资源,只要你在try()的括号内部定义要使用的资源。

前提是try()内的那些资源要实现自定义自动关闭类,这是新特性

try-with-resources statement。

请看例子:

jdk7之前

import java.io.*;//Copy from one file to another file character by character.//Pre-JDK 7 requires you to close the resources using a finally block.

public classFileCopyPreJDK7 {public static voidmain(String[] args) {

BufferedReader in= null;

BufferedWriter out= null;try{

in= new BufferedReader(new FileReader("in.txt"));

out= new BufferedWriter(new FileWriter("out.txt"));intcharRead;while ((charRead = in.read()) != -1) {

System.out.printf("%c ", (char)charRead);

out.write(charRead);

}

}catch(IOException ex) {

ex.printStackTrace();

}finally { //always close the streams

try{if (in != null) in.close();if (out != null) out.close();

}catch(IOException ex) {

ex.printStackTrace();

}

}try{

in.read();//Trigger IOException: Stream closed

} catch(IOException ex) {

ex.printStackTrace();

}

}

}

jdk7之后

import java.io.*;//Copy from one file to another file character by character.//JDK 7 has a try-with-resources statement, which ensures that//each resource opened in try() is closed at the end of the statement.

public classFileCopyJDK7 {public static voidmain(String[] args) {try (BufferedReader in = new BufferedReader(new FileReader("in.txt"));

BufferedWriter out= new BufferedWriter(new FileWriter("out.txt"))) {intcharRead;while ((charRead = in.read()) != -1) {

System.out.printf("%c ", (char)charRead);

out.write(charRead);

}

}catch(IOException ex) {

ex.printStackTrace();

}

}

}

JAVA7新增加了Objects类

它提供了一些方法来操作对象,这些方法大多数是“空指针”安全的,比如 Objects.toString(obj);如果obj是null,,则输出是null。

否则如果你自己用obj.toString(),如果obj是null,则会抛出NullPointerException.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值