赞
踩
JDK5新特性:
(1)自动装箱和拆箱:
- public class JDK5TNewFeatures {
-
- public static void main(String[] args) {
- Integer num = 10;
- int num2 = num;
- System.out.println(num2);
- }
-
- }
如下是反编译class文件获得的源码:实际上编译器自动完成了拆装箱,是Java的蜜糖。
- import java.io.PrintStream;
-
- public class JDK5TNewFeatures
- {
- public static void main(String[] args)
- {
- Integer num = Integer.valueOf(10);
- int num2 = num.intValue();
- System.out.println(num2);
- }
- }
(2)泛型:泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数。这种参数类型可以用在类、接口和方法的创建中,分别称为泛型类、泛型接口、泛型方法。 Java语言引入泛型的好处是安全简单。
ArrayList<String> list = new ArrayList<String>();
在Java SE 1.5之前,没有泛型的情况的下,通过对类型Object的引用来实现参数的“任意化”,“任意化”带来的缺点是要做显式的强制类型转换,而这种转换是要求开发者对实际参数类型可以预知的情况下进行的。对于强制类型转换错误的情况,编译器可能不提示错误,在运行的时候才出现异常,这是一个安全隐患。
泛型的好处是在编译的时候检查类型安全,并且所有的强制转换都是自动和隐式的,以提高代码的重用率。
(3)增强for循环:
- public class JDK5TNewFeatures {
-
- public static void main(String[] args) {
- ArrayList<String> list = new ArrayList<String>();
- list.add("hello");
- list.add("World");
- list.add("Is-Me-Hl");
- for (String s : list) {
- System.out.println(s);
- }
- }
-
- }
注意的是:增强for循环底层也是使用迭代器完成的遍历,只不过这个迭代器由JVM获取。在使用增强for循环的过程中,需要注意的是:不能使用集合对象对集合元素的个数进行修改。
迭代器遍历元素和增强for循环遍历元素的区别:迭代器遍历集合中可以使用remove方法删除集合元素,而增强for循环不可以。
普通for循环与挣钱for循环的区别:增强for循环一定有遍历的目标,而普通for循环可以没有。
如果在增强for循环中修改集合元素个数会报如下错误:
- public class JDK5TNewFeatures {
-
- public static void main(String[] args) {
- ArrayList<String> list = new ArrayList<String>();
- list.add("hello");
- list.add("World");
- list.add("Is-Me-Hl");
- for (String s : list) {
- // list.remove(s);
- list.add("Happy");
- }
- }
-
- }
- Exception in thread "main" java.util.ConcurrentModificationException
- at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
- at java.util.ArrayList$Itr.next(Unknown Source)
- at h.l.jdk5.JDK5TNewFeatures.main(JDK5TNewFeatures.java:12)
(4)静态导入:
import static java.lang.System.out;//导入java.lang包下的System类的静态方法out;
- import java.util.ArrayList;
- import static java.lang.System.out;
-
- public class JDK5TNewFeatures {
-
- public static void main(String[] args) {
- ArrayList<String> list = new ArrayList<String>();
- list.add("hello");
- list.add("World");
- list.add("Is-Me-Hl");
- for (String s : list) {
- out.println(s);
- }
- }
-
- }

import xxxx (非静态导入)和 import static xxxx(静态导入)的区别是前者一般导入的是类文件如import java.util.Scanner;后者一般是导入静态的方法,import static java.lang.System.out;
(5)可变参数:方法的参数个数不确定。用"..."定义。
- public class JDK5TNewFeatures {
-
- public static void main(String[] args) {
- sum(1, 2, 3);
- sum(4, 5, 6, 7, 8);
- sum(1);
- }
-
- private static void sum(int... is) {
- int sum = 0;
- for (int i = 0; i < is.length; i++) {
- sum += is[i];
- }
- System.out.println(sum);
- }
-
- }

(6)枚举:enum:有限个实例。
- public enum EnumDemo {
- Front, Behind, Left, Right;
- }
- public class JDK5TNewFeatures {
-
- public static void main(String[] args) {
- EnumDemo ed = EnumDemo.Front;
- System.out.println(ed);
- }
- }
- //执行结果
- Front
- public enum EnumDemo {
- Front("前"), Behind("后"), Left("左"), Right("右");
-
- private String name;
-
- private EnumDemo(String name) {
- this.name = name;
- }
-
- public String getName() {
- return name;
- }
-
- }
- public class JDK5TNewFeatures {
-
- public static void main(String[] args) {
- EnumDemo ed = EnumDemo.Front;
- System.out.println(ed);
- System.out.println(ed.getName());
- }
- }
- //执行结果
- Front
- 前
- public enum EnumDemo {
- Front("前") {
- @Override
- public void show() {
- System.out.println("前面");
- }
- },
- Behind("后") {
- @Override
- public void show() {
- System.out.println("后面");
- }
- },
- Left("左") {
- @Override
- public void show() {
- System.out.println("左面");
- }
- },
- Right("右") {
- @Override
- public void show() {
- System.out.println("右面");
- }
- };
-
- private String name;
-
- private EnumDemo(String name) {
- this.name = name;
- }
-
- public String getName() {
- return name;
- }
-
- public abstract void show();
- }

- public class JDK5TNewFeatures {
-
- public static void main(String[] args) {
- EnumDemo ed = EnumDemo.Front;
- System.out.println(ed);
- System.out.println(ed.getName());
- ed.show();
- }
- }
- //执行结果
- Front
- 前
- 前面
上述就是枚举的简单使用。对于枚举有一下几点要注意一下:
枚举也是类,编译后会生成.class文件。
枚举值默认的修饰符是public static final。
枚举类的构造函数只能使用private访问修饰符,如果省略了其构造器的访问控制符,则默认使用private修饰。
上述的Front、Behind等值,默认的写法都是:
- public static final EnumDemo Front= new EnumDemo ();
-
- public static final EnumDemo Behind= new EnumDemo ();
-
- public static final EnumDemo Left= new EnumDemo ();
-
- public static final EnumDemo Right= new EnumDemo ();
JDK7新特性:
(1)二进制字面量:
- public class JDK7TNewFeatures {
-
- public static void main(String[] args) {
- int a = 0b100_100_100;
- System.out.println(a);
- }
- }
(2)数字字面量可以出现下划线:
- public class JDK7TNewFeatures {
-
- public static void main(String[] args) {
- int a = 100_100_100;
- System.out.println(a);
- }
- }
(3)switch语句可以用字符串:
- public class JDK7TNewFeatures {
-
- public static void main(String[] args) {
- switch ("str1") {
- case "str1":
- System.out.println("str1");
- break;
- case "str2":
- System.out.println("str2");
- break;
- case "str3":
- System.out.println("str3");
- break;
- }
- }
- }

(4)泛型简化:
- public class JDK7TNewFeatures {
-
- public static void main(String[] args) {
- ArrayList<String> list = new ArrayList<>();
- }
- }
(5)异常的多个catch合并:
- public class JDK7TNewFeatures {
-
- public static void main(String[] args) {
- int[] a = { 1, 2, 3 };
- try {
- System.out.println(10 / 0);
- System.out.println(a[3]);
- } catch (ArithmeticException | ArrayIndexOutOfBoundsException e) {
- System.out.println("出问题了");
- }
- }
- }
(6)try...with...resources语句:
- public static void main(String[] args) {//原来的写法
- FileInputStream inputStream = null;
- try {
- inputStream = new FileInputStream(new File("test"));
- System.out.println(inputStream.read());
- } catch (IOException e) {
- throw new RuntimeException(e.getMessage(), e);
- } finally {
- if (inputStream != null) {
- try {
- inputStream.close();
- } catch (IOException e) {
- throw new RuntimeException(e.getMessage(), e);
- }
- }
- }
- }

- public static void main(String[] args) {//JDK7后使用
- try (FileInputStream inputStream = new FileInputStream(new File("test"))) {
- System.out.println(inputStream.read());
- } catch (IOException e) {
- throw new RuntimeException(e.getMessage(), e);
- }
- }
try-with-resource并不是JVM虚拟机的新增功能,只是JDK实现了一个语法糖,当你将上面代码反编译后会发现,其实对JVM虚拟机而言,它看到的依然是之前的写法:
- public static void main(String[] args) {
- try {
- FileInputStream inputStream = new FileInputStream(new File("test"));
- Throwable var2 = null;
-
- try {
- System.out.println(inputStream.read());
- } catch (Throwable var12) {
- var2 = var12;
- throw var12;
- } finally {
- if (inputStream != null) {
- if (var2 != null) {
- try {
- inputStream.close();
- } catch (Throwable var11) {
- var2.addSuppressed(var11);
- }
- } else {
- inputStream.close();
- }
- }
-
- }
-
- } catch (IOException var14) {
- throw new RuntimeException(var14.getMessage(), var14);
- }
- }

JDK8新特性:参考https://blog.csdn.net/u014470581/article/details/54944384
JDK10新特性:参考https://www.oschina.net/translate/109-new-features-in-jdk-10
注:以上文章仅是个人学习过程总结,若有不当之处,望不吝赐教。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。