JDK 1.5 - 1.7 版本特性
JDK 1.5
泛型
通过引入泛型,我们将获得编译时类型的安全和运行时更小地抛出 ClassCastExceptions的可能。在JDK1.5中,你可以声明一个集合将接收/返回的对象的类型。
之前:
List listOfEmployeeName = new ArrayList();
现在:
List<String> listOfEmployeeName = new ArrayList<String>();
增强for循环——foreach
更方便的遍历,免于使用for(int i = 0; i < list.size(); i++)
这样的复杂写法。
foreach语法格式
for(ElementType element:arrayName){};
自动置入置出(Autoboxing/unboxing)
主要处理Integer、Double等这样的包装类
之前
Integer age= new Integer(30);
Integer ageAfterTenYear= new Integer(age.intValue +10);
现在
Integer ageAfterTenYear= age +10; //自动解封
类型安全的枚举
之前
public class Font
{
public static final int COLOR_RED = 1;
public static final int COLOR_BLUE = 2;
.......
}
之前的缺点
一经改变,全部要重新编译;如果成员为String类型,比较起来也会比较麻烦,需要使用equals()
方法,这样效率比使用==
要低很多。
现在
enum Color
{
RED(1),BULE(2); //public static final 类型
private int num;
Color(int num) // private constructor
{
this.num = num;
}
}
现在的特点
- 构造器私有,用户无法实例化
- 除了static final的这几个成员,不会存在别的,所以我们在判断两个常量是否相等时,就可以直接使用
==
来判断,效率是比较高的。 - 提供了编译时的类型安全性,假如有一个function 的参数之一为Color类型,那么传入的参数除了null之外必须是static final域之一,不然编译就会出错
- 如果需要获取值的话,需要自定义函数
public int getValue() { return this.value; }
静态导入
之前
import org.yyy.pkg.Increment;
….
return var + Increment.INC;
现在
只要import了package,那么就可以不写类名直接调用
return var + INC
可变参数
参数个数不确定,但是类型确定的情况下,Java把可变参数当做数组处理
public class Main
{
private static int add(int a, int... args)
{
int ret = 0;
for (Integer num : args)
ret += num;
return ret;
}
public static void main(String[] args)
{
System.out.println(add(1));
System.out.println(add(1, 2));
System.out.println(add(1, 2, 3));
}
}
内省
Java JDK中提供了一套 API(如 PropertyDescriptor) 用来访问某个属性的 getter/setter 方法,这就是内省。
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
class StuInfo
{
private String name;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
@Override
public String toString()
{
return "StuInfo [name=" + name + "]";
}
}
public class Main
{
public static void main(String[] args) throws Exception
{
StuInfo stuInfo = new StuInfo();
PropertyDescriptor namePropertyDescriptor = new PropertyDescriptor(
"name", StuInfo.class);
Method setNameMethod = namePropertyDescriptor.getWriteMethod();
setNameMethod.invoke(stuInfo, "Rafe");
System.out.println(stuInfo);
}
}
JDK 1.6
web服务元数据
J2EE相关的优化,详情见参考文献
脚本语言的支持
可以利用ScriptEngine API在Java里面编写并执行脚本程序,首先创建一个ScriptEngineManager对象,再通过ScriptEngineManager获得ScriptEngine对象,最后用ScriptEngine的eval方法执行脚本。
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class Main
{
public static void main(String[] args) throws Exception
{
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
engine.eval("print(\"HelloWorld\")");
}
}
JTable的排序和过滤
原来的JTable基本上是只能显示数据,在JDK6新增了对JTable的排序和过滤功能
更简单,更强大的JAX-WS
一种用Java和XML开发Web Services应用程序的框架,是Java Architecture for XML Web Services的缩写
轻量级Http Server
JDK6提供了一个简单的Http Server API,据此我们可以构建自己的嵌入式Http Server
嵌入式数据库 Derby
是一个pure java的数据库,现在已经被列入到java1.6中。
JDK 1.7
switch中可以使用字串了
public class Main
{
public static void main(String[] args) throws Exception
{
String line = "condition_2";
switch (line)
{
case "condition_1":
System.out.println("condition_1");
break;
case "condition_2":
System.out.println("condition_2");
break;
}
}
}
泛型实例化类型自动推断
List<String> tempList = new ArrayList<>(); // <>也称为钻石运算符Diamond Operator
自动资源管理
Java 7 的 try可以自动调用资源的close函数。
之前
public class Main
{
public static void main(String[] args)
{
try
{
BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in));
String line = reader.readLine();
reader.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
}
现在
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in));)
{
String line = reader.readLine();
} catch (Exception e)
{
e.printStackTrace();
}
带下划线的整型字面量
int million = 1_000_000
改良的异常处理
Java 7 推出了对异常的“multi-catch”功能,简化了处理方式
之前
public class Main
{
public static void main(String[] args)
{
try
{
myMethod();
} catch (IOException e)
{
e.printStackTrace();
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
}
}
private static void myMethod() throws IOException,IndexOutOfBoundsException
{
}
}
现在
public class Main
{
public static void main(String[] args)
{
try
{
myMethod();
} catch (IOException | IndexOutOfBoundsException e)
{
e.printStackTrace();
}
}
private static void myMethod() throws IOException,IndexOutOfBoundsException
{
}
}
新文件系统API——NIO 2.0
NIO 2.0 带来了很多增强功能。它同时推出了几个新的class来帮助开发者减轻处理多文件系统时的工作压力。详见API文档。
Fork and Join
fork/join框架是用多线程的方式实现分治法来解决问题。fork指的是将问题不断地缩小规模,join是指根据子问题的计算结果,得出更高层次的结果。具体请看大神的总结Java线程之fork/join框架
支持动态
Java7推出了一个新的特性“invoke dynamic”。这然JVM变得可以去包含非Java语言的需求。一个新的包java.lang.invoke已经被创建去帮助拓展对动态语言的支持。
网传有误部分
JDK 1.7
“语法上支持集合,而不一定是数组”不存在
亲测无效,编译不通过,报语法错误
“新增一些取环境信息的工具方法”有误
以下这些:
- File System.getJavaIoTempDir() // IO临时文件夹
- File System.getJavaHomeDir() // JRE的安装目录
- File System.getUserHomeDir() // 当前用户目录
- File System.getUserDir() // 启动java进程时所在的目录
都是来着org.apache.commons.lang3.SystemUtils,不是JDK的特性。这里误用了文章What methods should go into a java.util.Objects class in JDK 7的内容。
“Boolean类型反转,空指针安全,参与位运算”有误
关于
- 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)
- Boolean Booleans.negate(Boolean booleanObj)
JDK 中并没有Booleans这样的类。这些方法都是出自一篇名为What methods should go into a java.util.Objects class in JDK 7的文章,注意文章的标题,这只是一封建议信,并不是最后JDK 1.7 的方案。
“两个char间的equals”有误
理由同上,同样滥用了What methods should go into a java.util.Objects class in JDK 7里面的内容。
“安全的加减乘除 ” 有误
理由同上,同样滥用了What methods should go into a java.util.Objects class in JDK 7里面的内容。