|
1.自动装箱与拆箱(autoboxing and unboxing) 实现了基本类型与外覆类之间的隐式转换。基本类型至外覆类的转换称为装箱,外覆类至基本类型的转换为解箱。
2.泛型(Generic) 增强了java的类型安全,可以在编译期间对容器内的对象进行类型检查,在运行期不必进行类型的转换。而在j2se5之前必须在运行期动态进行容器内对象的检查及转换,减少含糊的容器,可以定义什么类型的数据放入容器
3.注解(Meta data)
4.增强循环(Enhanced for Loop)
5.可变参数(Variable Arguments)
6.静态导入(Static Imports)
java 代码
- import static java.lang.Math.*;
-
- sqrt(pow(x, 2) + pow(y, 2));
其中import static java.lang.Math.*;就是静态导入的语法,它的意思是导入Math类中的所有static方法和属性。这样我们在使用这些方法和属性时就不必写类名。需要注意的是默认包无法用静态导入,另外如果导入的类中有重复的方法和属性则需要写出类名,否则编译时无法通过。
7.枚举类(Enumeration Classes)
用法:public enum Name {types, ….}
java 代码
- public enum Colors {Red, Yellow, Blue, Orange, Green, Purple, Brown, Black}
-
- public static void main(String[] args){
- Colors myColor = Colors.Red;
- System.out.println(myColor);
- }
8.Building Strings(StringBuilder类) 在JDK5.0中引入了StringBuilder类,该类的方法不是同步(synchronized)的,这使得它比StringBuffer更加轻量级和有效。
9.控制台输入(Console Input) 在JDK5.0之前我们只能通过JOptionPane.showInputDialog进行输入,但在5.0中我们可以通过类Scanner在控制台进行输入操作
java 代码
- java.util.Scanner s = new Scanner(System.in);
- System.out.println("s:");
- System.out.println(s.next());
10.Covariant Return Types
JDK5之前我们覆盖一个方法时我们无法改变被方法的返回类型,但在JDK5中我们可以改变它
java 代码
- public Employee clone() { ... }
-
- ...
- Employee cloned = e.clone();
11.格式化I/O(Formatted I/O) 类似C的格式化输入输出
java 代码
- public class TestFormat{
-
- public static void main(String[] args){
- int a = 150000, b = 10;
- float c = 5.0101f, d = 3.14f;
- System.out.printf("%4d %4d%n", a, b);
- System.out.printf("%x %x%n", a, b);
- System.out.printf("%3.2f %1.1f%n", c, d);
- System.out.printf("%1.3e %1.3e%n", c, d*100);
- }
- }
|