1.jdk9
1.1 模块化机制
定义模块:module-info.java
module com.newfeature.test
{requires java.se;requires lombok;requires junit;
}
package com.newfeature.test;import java.lang.reflect.Field;public class Main {public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {Class<String> stringClass=String.class;Field field=stringClass.getDeclaredField("value");field.setAccessible(true);System.out.println(field.get("ABC"));}
}
输出结果:
Exception in thread "main" java.lang.reflect.InaccessibleObjectException: Unable to make field private final byte[] java.lang.String.value accessible: module java.base does not "opens java.lang" to module com.newfeature.testat java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:178)at java.base/java.lang.reflect.Field.setAccessible(Field.java:172)at com.newfeature.test/com.newfeature.test.Main.main(Main.java:10)
暴露如上错误:需要开放模块的反射权限
java --list-modules 列出所有模块
open module com.newfeature.test //open开发模块反射权限
{requires java.se;requires lombok; //requires 导入模块requires transitive junit; //定义模块的传递引用//opens com.newfeature.test; //opens开放模块权限给其他模块uses com.newfeature.test.Test;//uses 指定模块使用的抽象类或接口实现provides com.newfeature.test.Test with com.newfeature.test.TestImpl;//提供接口的实现类
}
1.2 jshell交互式编程
1.3 接口中支持private方法
- jdk8接口中支持的默认方法
public interface Test {/*** 定义jdk8接口中支持的默认方法*/default void test(){System.out.println("test方法默认实现");}}
-
jdk9,接口中可以有私有方法,私有方法必须有方法体,只有接口中默认其他方法可以调用
package com.newfeature.test;public interface Test {private static void inner(){System.out.println("接口中私有方法");}static void add(){inner();//todo} }
1.4 集合类新增工厂方法
package com.newfeature.test;import java.util.HashMap;
import java.util.Map;public class MapDemo1 {public static void main(String[] args) {//创建map对象方式1Map<String,Integer> map=new HashMap<>();map.put("AAA",12);map.put("BBB",13);map.put("CCC",13);System.out.println(map);//jdk9 之后,通过of方法快速创建map对象,是只读的,不可以修改Map<String,Integer> map2=Map.of("AAA",18,"BBB",19);System.out.println(map2);map2.put("ddd",18);System.out.println(map2);}
}
输出结果:
{AAA=12, CCC=13, BBB=13}
{BBB=19, AAA=18}
Exception in thread "main" java.lang.UnsupportedOperationExceptionat java.base/java.util.ImmutableCollections.uoe(ImmutableCollections.java:142)at java.base/java.util.ImmutableCollections$AbstractImmutableMap.put(ImmutableCollections.java:1072)at com.newfeature.test/com.newfeature.test.MapDemo1.main(MapDemo1.java:20)
List<String> str=List.of("2","4","6");
Set<Integer> set=Set.of(5, 6,9);
System.out.println(str);
System.out.println(set);
输出:
[2, 4, 6]
[9, 6, 5]
注意:输出顺序和 添加顺序不一样。也是只读的
1.5 jdk9对stream做了 改进
package com.newfeature.test;import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;public class MapDemo1 {public static void main(String[] args) {//jdk8Stream.of(null).forEach(System.out::println);Stream.ofNullable(null) //jdk9新增的ofNullable方法,出现传入null不会报错.forEach(System.out::println);}
}
输出结果:
jdk8
Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "array" is nullat java.base/java.util.Arrays.stream(Arrays.java:5428)at java.base/java.util.stream.Stream.of(Stream.java:1426)at com.newfeature.test/com.newfeature.test.MapDemo1.ma
jdk9 输出无错误
- 迭代输出
package com.newfeature.test;import java.util.stream.Stream;public class MapDemo1 {public static void main(String[] args) {//jkd8 stream 迭代输出Stream.iterate(0,i->i+1).limit(20) //限制生成20个.forEach(System.out::println);System.out.println("-----------------------------------");//jdk9 stream迭代输出 ,参考:for(int i=0;i<20;i++)Stream.iterate(0,i->i<20,i->i+1).forEach(System.out::println);}}
-
数据截断
-
package com.newfeature.test;import java.util.stream.Stream;public class MapDemo1 {public static void main(String[] args) {Stream.iterate(0,i->i<20,i->i+1).takeWhile(i-> i<10) //大于10就截断.forEach(System.out::println);} }
输出:
0
1
2
3
4
5
6
7
8
9
- 跳过一定的数据
package com.newfeature.test;import java.util.stream.Stream;public class MapDemo1 {public static void main(String[] args) {Stream.iterate(0,i->i<20,i->i+1).dropWhile(i-> i<10) //大于10就输出.forEach(System.out::println);}}
输出:
10
11
12
13
14
15
16
17
18
19
- jdk9新增 ifPresentOrElse 判空处理
package com.newfeature.test;import java.util.Optional;public class MapDemo1 {public static void main(String[] args) {String str=null;Optional.ofNullable(str).ifPresentOrElse(s->{System.out.println("s="+s);},()->{System.out.println("s为空了");});}}
-
or方法替换optional类
-
package com.newfeature.test;import java.util.Optional;public class MapDemo1 {public static void main(String[] args) {String str=null;Optional.ofNullable(str).or(()->Optional.of("AAA"))//如果当前被包装的类不是null,返回自己,但是如果为null返回supplier提供另一个optional类(即可以传入一个默认值).ifPresent(System.out::println);} }
2.jdk10新特性
2.1 局部变量类型推断
package com.newfeature.test;public class MapDemo1 {public static void main(String[] args) {var s="hello";System.out.println(s);System.out.println(s.getClass());}
}
输出结果:
hello
class java.lang.String
注意:var必须定义变量必须有初始值,才可以推断类型;var仅使用于局部变量定义
3.jdk11新特性
3.1 lambda形参局部变量
package com.newfeature.test;import java.util.function.Consumer;public class MapDemo1 {public static void main(String[] args) {Consumer<String> consumer=(var s)->{System.out.println(s);};consumer.accept("AAA");}
}
3.2 String类的方法增强
package com.newfeature.test;public class MapDemo1 {public static void main(String[] args) {String str=" ";System.out.println(str.isBlank());//判断全部是否都是空格String hi="world";System.out.println(hi.repeat(3));//让字符hi重复3次String ln="AB\nC\nD";System.out.println(ln.lines().count());//lines()进行换行String strr=" A B C D ";System.out.println(strr.strip());//去除首尾空格System.out.println(strr.stripLeading());//去掉首部空格System.out.println(strr.stripTrailing());//去掉尾部空格}
}
3.3 全新的httpclient api
public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException {HttpClient client=HttpClient.newHttpClient();HttpRequest request = HttpRequest.newBuilder(new URI("https://www.baidu.com")).GET().build();HttpResponse<String> response=client.send(request, HttpResponse.BodyHandlers.ofString());System.out.println(response.body());}
4 jdk 12~17 switch表达式
/*** switch表达式使用* @param score* @return*/public static String grade(int score){score /= 10;return switch (score){case 10,9 ->"优秀";case 8,7 ->"良好";case 6->"及格";default -> "不及格";};}/*** switch表达式使用* @param score* @return*/public static String grade1(int score){score /= 10;return switch (score){case 10,9 ->"优秀";case 8,7 ->"良好";case 6->"及格";default -> {System.out.println("判决及格与否");yield "不及格";}};}public static String grade3(int score){score /= 10;return switch (score){case 10,9:yield "优秀";case 8,7: yield "良好";case 6 :yield"及格";default:System.out.println("判决及格与否");yield "不及格";};}