总结于尚硅谷学习视频
https://www.bilibili.com/video/BV1Kb411W75N?p=694
介绍
新特性一:jdk目录结构的改变
https://www.bilibili.com/video/BV1Kb411W75N?p=695
jdk8的目录结构
jdk9的目录结构
新特性二:模块化系统
https://www.bilibili.com/video/BV1Kb411W75N?p=696
图片
新特性三:java的REPL工具:jshell
https://www.bilibili.com/video/BV1Kb411W75N?p=697
图片
新特性四:接口中声明私有方法
https://www.bilibili.com/video/BV1Kb411W75N?p=698
代码
MyInterface接口
package com.day0330_2; public interface MyInterface { //如下的三个方法的权限修饰符都是public void methodAbstract(); static void methodStatic(){ System.out.println("我是接口中的静态方法"); } default void methodDefault(){ System.out.println("我是接口中的默认方法"); methodPrivate(); } //java9接口中允许出现私有方法 private void methodPrivate(){ System.out.println("我是接口中的私有方法"); } }
MyInterfaceImpl类
package com.day0330_2; public class MyInterfaceImpl implements MyInterface{ @Override public void methodAbstract() { } @Override public void methodDefault() { System.out.println("实现类重写了接口中的默认方法"); } public static void main(String[] args) { //接口中的静态方法只能由自己调用 MyInterface.methodStatic(); //接口的实现类不能调用接口的静态方法 // MyInterfaceImpl.methodStatic(); MyInterfaceImpl imp=new MyInterfaceImpl(); imp.methodDefault(); //接口的私有方法只能由接口自己调用 // imp.methodPrivate(); } }
新特性五:钻石操作符的语法升级
https://www.bilibili.com/video/BV1Kb411W75N?p=699
新特性六:try结构的语法升级
https://www.bilibili.com/video/BV1Kb411W75N?p=700
图片
代码五、六
java9Test类
package com.day0330_2; import org.junit.jupiter.api.Test; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Comparator; public class java9Test { //java特性五:钻石操作符的升级 @Test public void test2() { //钻石操作符与匿名内部类在java8中不能共存。在java9中可以 Comparator<Object> com = new Comparator<>() { @Override public int compare(Object o1, Object o2) { return 0; } }; //jdk7的新特性:类型推断 ArrayList<String> list = new ArrayList<>(); } //java9 特性六:try操作的升级 public static void main(String[] args) { //java8之前的资源关闭操作 // InputStreamReader reader= null; // try { // reader = new InputStreamReader(System.in); // char[] cbuf=new char[20]; // int len; // if((len=reader.read())!=-1){ // String str=new String(cbuf,0,len); // System.out.println(str); // } // } catch (IOException e) { // e.printStackTrace(); // } finally { // if(reader!=null){ // try { // reader.close(); // } catch (IOException e) { // e.printStackTrace(); // } // } // } //java8中的资源关闭操作 //要求自动关闭的资源的实例化必须放在一对小括号里 // try (InputStreamReader reader=new InputStreamReader(System.in)){ // char[] cbuf=new char[20]; // int len; // if((len=reader.read())!=-1) { // String str = new String(cbuf, 0, len); // System.out.println(str); // } // } catch (IOException e) { // e.printStackTrace(); // } //java9中的资源关闭操作 //要求自动关闭的资源的实例化可以放在一对小括号外。 //此时的资源属性是常量,声明为final的,不可修改 InputStreamReader reader = new InputStreamReader(System.in); try (reader) { char[] cbuf = new char[20]; int len; if ((len = reader.read()) != -1) { String str = new String(cbuf, 0, len); System.out.println(str); } } catch (IOException e) { e.printStackTrace(); } } }
新特性七:String底层存储结构的变更
https://www.bilibili.com/video/BV1Kb411W75N?p=701
图片
新特性八:工厂方法创建只读
https://www.bilibili.com/video/BV1Kb411W75N?p=702
新特性九:InputStream的transferTo()方法
https://www.bilibili.com/video/BV1Kb411W75N?p=703
代码八、九
Java9Test1类
package com.day0330_2; import org.junit.jupiter.api.Test; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.*; public class Java9Test1 { //java8中的写法 @Test public void test1(){ List <String> nameList=new ArrayList<>(); nameList.add("Joe"); nameList.add("Bob"); nameList.add("Bill"); //返回的nameList是只读的集合 nameList= Collections.unmodifiableList(nameList); nameList.add("Tom");//UnsupportedOperationException System.out.println(nameList); } @Test public void test2(){ List<String> list=Collections.unmodifiableList(Arrays.asList("a","b","c")); Set<String> set=Collections.unmodifiableSet(new HashSet<>(Arrays.asList("a","b","c"))); //如下操作不适用与jdk8及之前的版本 Map<String,Integer> map=Collections.unmodifiableMap(new HashMap<>(){ { put("a", 1); put("b", 2); put("c", 3); } }); map.forEach((k,v)->System.out.println(k+":"+v)); } @Test public void test3(){ //此时得到的集合是只读集合 List<Integer> list=Arrays.asList(1,2,3,4,5); //报异常 list.add(6); } //java9新特性八:集合工厂方法:创建只读集合 @Test public void test4(){ List<Integer> list1 = List.of(1, 2, 3, 4, 5); //不能添加 // list1.add(6); System.out.println(list1); Set<Integer> set1 = Set.of(23, 3, 54, 65, 43, 76, 34, 46); //不能添加 // set1.add(4); System.out.println(set1); Map<String, Integer> map1 = Map.of("Tom", 23, "Jerry", 54, "HanMeimei", 12); //不能添加 // map1.put("Lilei",34); System.out.println(map1); Map<String, Integer> map2 = Map.ofEntries(Map.entry("Tom", 23), Map.entry("Jerry", 54)); // map2.put("Lilei",34); System.out.println(map2); } //java9新特性九:InputStream的新方法:tranferTo() @Test public void test5(){ ClassLoader c1=this.getClass().getClassLoader(); try(InputStream is=c1.getResourceAsStream("hello.txt"); OutputStream os=new FileOutputStream("src\\hello.txt");){ is.transferTo(os);//把流中的所有数据直接自动的复制到输出流 }catch (IOException e){ e.printStackTrace(); } } }
新特性十:StreamAPI新增的四个方法
https://www.bilibili.com/video/BV1Kb411W75N?p=704
新特性十一:Optional的新方法stream()
https://www.bilibili.com/video/BV1Kb411W75N?p=705
代码十、十一
java9Test2类
package com.day0330_2; import org.junit.jupiter.api.Test; import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Optional; import java.util.stream.Stream; public class java9Test2 { //java9新特性:StreamAPI的加强 @Test public void test1(){ List<Integer> list = Arrays.asList(23, 43, 45, 55, 61, 54, 32, 2, 45, 89, 7); //takeWhile 返回从开头开始的按照指定规则尽量多的元素 // list.stream().takeWhile(x->x<60).forEach(System.out::println);//23, 43, 45, 55 //dropWhile:与takeWhile相反,返回剩余元素 list.stream().dropWhile(x->x<60).forEach(System.out::println);//61, 54, 32, 2, 45, 89, 7 } @Test public void test2(){ //of()参数中有多个元素,可以包含null Stream<Integer> stream1=Stream.of(1,2,3,null); stream1.forEach(System.out::println); //of()参数不能存储单个null值。否则报异常 // Stream<Object> stream2 = Stream.of(null); // stream2.forEach(System.out::println); Integer i=10; i=null; // ofNullable():形参变量时可以为null值的单个元素 Stream<Integer> stream3 = Stream.ofNullable(i); long count = stream3.count(); System.out.println(count); } @Test public void test3(){ Stream.iterate(0,x->x+1).limit(10).forEach(System.out::println); //java9中重载的方法 Stream.iterate(0,x->x<100,x->x+1).forEach(System.out::println); } //java9新特性十一:Optional提供了新的方法stream() @Test public void test4(){ List<String> list=new ArrayList<>(); list.add("Tom"); list.add("Jerry"); list.add("Tim"); Optional<List<String>> optional =Optional.ofNullable(list); Stream<List<String>> stream = optional.stream(); // long count = stream.count(); // System.out.println(count); stream.flatMap(x->x.stream()).forEach(System.out::println); } }
新特性十二:升级的Nashorn引擎
https://www.bilibili.com/video/BV1Kb411W75N?p=706












