当前位置:   article > 正文

Java基础总结之各版本JDK新特性_java各版本jdk

java各版本jdk

JDK5新特性:

(1)自动装箱和拆箱:

  1. public class JDK5TNewFeatures {
  2. public static void main(String[] args) {
  3. Integer num = 10;
  4. int num2 = num;
  5. System.out.println(num2);
  6. }
  7. }

如下是反编译class文件获得的源码:实际上编译器自动完成了拆装箱,是Java的蜜糖。

  1. import java.io.PrintStream;
  2. public class JDK5TNewFeatures
  3. {
  4. public static void main(String[] args)
  5. {
  6. Integer num = Integer.valueOf(10);
  7. int num2 = num.intValue();
  8. System.out.println(num2);
  9. }
  10. }

(2)泛型:泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数。这种参数类型可以用在类、接口和方法的创建中,分别称为泛型类、泛型接口、泛型方法。 Java语言引入泛型的好处是安全简单。

ArrayList<String> list = new ArrayList<String>();

在Java SE 1.5之前,没有泛型的情况的下,通过对类型Object的引用来实现参数的“任意化”,“任意化”带来的缺点是要做显式的强制类型转换,而这种转换是要求开发者对实际参数类型可以预知的情况下进行的。对于强制类型转换错误的情况,编译器可能不提示错误,在运行的时候才出现异常,这是一个安全隐患。 

泛型的好处是在编译的时候检查类型安全,并且所有的强制转换都是自动和隐式的,以提高代码的重用率。

(3)增强for循环:

  1. public class JDK5TNewFeatures {
  2. public static void main(String[] args) {
  3. ArrayList<String> list = new ArrayList<String>();
  4. list.add("hello");
  5. list.add("World");
  6. list.add("Is-Me-Hl");
  7. for (String s : list) {
  8. System.out.println(s);
  9. }
  10. }
  11. }

注意的是:增强for循环底层也是使用迭代器完成的遍历,只不过这个迭代器由JVM获取。在使用增强for循环的过程中,需要注意的是:不能使用集合对象对集合元素的个数进行修改。

迭代器遍历元素和增强for循环遍历元素的区别:迭代器遍历集合中可以使用remove方法删除集合元素,而增强for循环不可以。

普通for循环与挣钱for循环的区别:增强for循环一定有遍历的目标,而普通for循环可以没有。

如果在增强for循环中修改集合元素个数会报如下错误:

  1. public class JDK5TNewFeatures {
  2. public static void main(String[] args) {
  3. ArrayList<String> list = new ArrayList<String>();
  4. list.add("hello");
  5. list.add("World");
  6. list.add("Is-Me-Hl");
  7. for (String s : list) {
  8. // list.remove(s);
  9. list.add("Happy");
  10. }
  11. }
  12. }
  1. Exception in thread "main" java.util.ConcurrentModificationException
  2. at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
  3. at java.util.ArrayList$Itr.next(Unknown Source)
  4. at h.l.jdk5.JDK5TNewFeatures.main(JDK5TNewFeatures.java:12)

(4)静态导入:

import static java.lang.System.out;//导入java.lang包下的System类的静态方法out;
  1. import java.util.ArrayList;
  2. import static java.lang.System.out;
  3. public class JDK5TNewFeatures {
  4. public static void main(String[] args) {
  5. ArrayList<String> list = new ArrayList<String>();
  6. list.add("hello");
  7. list.add("World");
  8. list.add("Is-Me-Hl");
  9. for (String s : list) {
  10. out.println(s);
  11. }
  12. }
  13. }

 import xxxx (非静态导入)和 import static xxxx(静态导入)的区别是前者一般导入的是类文件如import java.util.Scanner;后者一般是导入静态的方法,import static java.lang.System.out;

(5)可变参数:方法的参数个数不确定。用"..."定义。

  1. public class JDK5TNewFeatures {
  2. public static void main(String[] args) {
  3. sum(1, 2, 3);
  4. sum(4, 5, 6, 7, 8);
  5. sum(1);
  6. }
  7. private static void sum(int... is) {
  8. int sum = 0;
  9. for (int i = 0; i < is.length; i++) {
  10. sum += is[i];
  11. }
  12. System.out.println(sum);
  13. }
  14. }

(6)枚举:enum:有限个实例。

  1. public enum EnumDemo {
  2. Front, Behind, Left, Right;
  3. }
  1. public class JDK5TNewFeatures {
  2. public static void main(String[] args) {
  3. EnumDemo ed = EnumDemo.Front;
  4. System.out.println(ed);
  5. }
  6. }
  7. //执行结果
  8. Front

  1. public enum EnumDemo {
  2. Front("前"), Behind("后"), Left("左"), Right("右");
  3. private String name;
  4. private EnumDemo(String name) {
  5. this.name = name;
  6. }
  7. public String getName() {
  8. return name;
  9. }
  10. }
  1. public class JDK5TNewFeatures {
  2. public static void main(String[] args) {
  3. EnumDemo ed = EnumDemo.Front;
  4. System.out.println(ed);
  5. System.out.println(ed.getName());
  6. }
  7. }
  8. //执行结果
  9. Front

  1. public enum EnumDemo {
  2. Front("前") {
  3. @Override
  4. public void show() {
  5. System.out.println("前面");
  6. }
  7. },
  8. Behind("后") {
  9. @Override
  10. public void show() {
  11. System.out.println("后面");
  12. }
  13. },
  14. Left("左") {
  15. @Override
  16. public void show() {
  17. System.out.println("左面");
  18. }
  19. },
  20. Right("右") {
  21. @Override
  22. public void show() {
  23. System.out.println("右面");
  24. }
  25. };
  26. private String name;
  27. private EnumDemo(String name) {
  28. this.name = name;
  29. }
  30. public String getName() {
  31. return name;
  32. }
  33. public abstract void show();
  34. }
  1. public class JDK5TNewFeatures {
  2. public static void main(String[] args) {
  3. EnumDemo ed = EnumDemo.Front;
  4. System.out.println(ed);
  5. System.out.println(ed.getName());
  6. ed.show();
  7. }
  8. }
  9. //执行结果
  10. Front
  11. 前面

上述就是枚举的简单使用。对于枚举有一下几点要注意一下:

枚举也是类,编译后会生成.class文件。

枚举值默认的修饰符是public static final。

枚举类的构造函数只能使用private访问修饰符,如果省略了其构造器的访问控制符,则默认使用private修饰。

上述的Front、Behind等值,默认的写法都是:

  1. public static final EnumDemo Front= new EnumDemo ();
  2. public static final EnumDemo Behind= new EnumDemo ();
  3. public static final EnumDemo Left= new EnumDemo ();
  4. public static final EnumDemo Right= new EnumDemo ();

JDK7新特性:

(1)二进制字面量:

  1. public class JDK7TNewFeatures {
  2. public static void main(String[] args) {
  3. int a = 0b100_100_100;
  4. System.out.println(a);
  5. }
  6. }

(2)数字字面量可以出现下划线:

  1. public class JDK7TNewFeatures {
  2. public static void main(String[] args) {
  3. int a = 100_100_100;
  4. System.out.println(a);
  5. }
  6. }

(3)switch语句可以用字符串:

  1. public class JDK7TNewFeatures {
  2. public static void main(String[] args) {
  3. switch ("str1") {
  4. case "str1":
  5. System.out.println("str1");
  6. break;
  7. case "str2":
  8. System.out.println("str2");
  9. break;
  10. case "str3":
  11. System.out.println("str3");
  12. break;
  13. }
  14. }
  15. }

(4)泛型简化:

  1. public class JDK7TNewFeatures {
  2. public static void main(String[] args) {
  3. ArrayList<String> list = new ArrayList<>();
  4. }
  5. }

(5)异常的多个catch合并:

  1. public class JDK7TNewFeatures {
  2. public static void main(String[] args) {
  3. int[] a = { 1, 2, 3 };
  4. try {
  5. System.out.println(10 / 0);
  6. System.out.println(a[3]);
  7. } catch (ArithmeticException | ArrayIndexOutOfBoundsException e) {
  8. System.out.println("出问题了");
  9. }
  10. }
  11. }

(6)try...with...resources语句:

  1. public static void main(String[] args) {//原来的写法
  2. FileInputStream inputStream = null;
  3. try {
  4. inputStream = new FileInputStream(new File("test"));
  5. System.out.println(inputStream.read());
  6. } catch (IOException e) {
  7. throw new RuntimeException(e.getMessage(), e);
  8. } finally {
  9. if (inputStream != null) {
  10. try {
  11. inputStream.close();
  12. } catch (IOException e) {
  13. throw new RuntimeException(e.getMessage(), e);
  14. }
  15. }
  16. }
  17. }
  1. public static void main(String[] args) {//JDK7后使用
  2. try (FileInputStream inputStream = new FileInputStream(new File("test"))) {
  3. System.out.println(inputStream.read());
  4. } catch (IOException e) {
  5. throw new RuntimeException(e.getMessage(), e);
  6. }
  7. }

try-with-resource并不是JVM虚拟机的新增功能,只是JDK实现了一个语法糖,当你将上面代码反编译后会发现,其实对JVM虚拟机而言,它看到的依然是之前的写法:

  1. public static void main(String[] args) {
  2. try {
  3. FileInputStream inputStream = new FileInputStream(new File("test"));
  4. Throwable var2 = null;
  5. try {
  6. System.out.println(inputStream.read());
  7. } catch (Throwable var12) {
  8. var2 = var12;
  9. throw var12;
  10. } finally {
  11. if (inputStream != null) {
  12. if (var2 != null) {
  13. try {
  14. inputStream.close();
  15. } catch (Throwable var11) {
  16. var2.addSuppressed(var11);
  17. }
  18. } else {
  19. inputStream.close();
  20. }
  21. }
  22. }
  23. } catch (IOException var14) {
  24. throw new RuntimeException(var14.getMessage(), var14);
  25. }
  26. }

JDK8新特性:参考https://blog.csdn.net/u014470581/article/details/54944384


JDK10新特性:参考https://www.oschina.net/translate/109-new-features-in-jdk-10


注:以上文章仅是个人学习过程总结,若有不当之处,望不吝赐教。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/53834
推荐阅读
相关标签
  

闽ICP备14008679号