JDK8较JDK7部分新特性
来源:互联网 发布:李剑模糊阅读知乎 编辑:程序博客网 时间:2024/06/06 09:26
参考:浅谈JDK8相对于JDK7的一些新特性
本文所有代码在JDK1.8 中运行
特性一、接口的默认方法
在JDK8中,允许给接口本身添加一个默认的实现。用“default”进行修饰。
/** * Created by user on 2017/9/4. */public interface MyComputer { default int sum(int a,int b){ return a+b; } int subtraction(int a,int b);}
public class test2170814 implements MyComputer{ public int subtraction(int a, int b) { return a-b; } public static void main(String[] args){ MyComputer a = new test2170814() ; System.out.println(a.sum(1,3)); System.out.println(a.subtraction(3,2)); }}
运行结果:
4
1
特性二、静态方法与构造函数的引用
- 静态方法引用
用来存储这个引用的类型用@FunctionlaInterface注解来标识。
//@param <F> 传值类型//@param <T> 结果类型public class test1 { public static void main(String[] args){ Fun<String,String> fun = test1::testMethod; System.out.println(fun.run("This is a test")); } @FunctionalInterface interface Fun<F,T>{ T run(F from); } public static String testMethod(String args){ return args; }}
运行结果:
This is a test
test1::testMethod 表示test1类中的testMethod方法
- 构造函数的引用
public class test1 { public static void main(String[] args){ Fun2<test1> mainFun = test1::new; mainFun.run("Creating"); } @FunctionalInterface interface Fun2<F extends test1>{ F run(String args); } public test1(String arg){ System.out.println(arg); }}
运行结果:
Creating
特性三、Lambda表达式
在JDK8中,引入了Lambda
import java.util.Arrays;import java.util.Collections;import java.util.List;public class test1 { public static void main(String[] args){ List<String> list = Arrays.asList("asd","dweas","aw","trs"); Collections.sort(list,(str1,str2)-> str1.compareTo(str2)); for(String i :list){ System.out.println(i); } }}
运行结果:
asd
aw
dweas
trs
上述为升序
再来看看降序:
import java.util.Arrays;import java.util.Collections;import java.util.List;public class test1 { public static void main(String[] args){ List<String> list = Arrays.asList("asd","dweas","aw","trs"); Collections.sort(list,(str1,str2)-> str2.compareTo(str1)); for(String i :list){ System.out.println(i); } }}
运行结果:
trs
dweas
aw
asd
核心代码:Collections.sort(list,(str1,str2)-> str2.compareTo(str1));
特性四、反射加强
它允许你直接通过反射获取参数的名字
import java.lang.reflect.Method;import java.lang.reflect.Parameter;public class ParameterNames { public static void main(String[] args) throws Exception { Method method = ParameterNames.class.getMethod( "main", String[].class ); for( final Parameter parameter: method.getParameters() ) { System.out.println( "Parameter: " + parameter.getName() ); } }}
如果不使用–parameters参数来编译这个类,然后运行这个类,会得到下面的输出:
Parameter: arg0
如果使用–parameters参数来编译这个类,程序的结构会有所不同(参数的真实名字将会显示出来):
Parameter: args
此外,Parameter类有一个很方便的方法isNamePresent()来验证是否可以获取参数的名字。
特性五、JVM新特性
PermGen空间被移除了,取而代之的是Metaspace(JEP
122)。JVM选项-XX:PermSize与-XX:MaxPermSize分别被-XX:MetaSpaceSize与-XX:MaxMetaspaceSize所代替。
阅读全文
0 0
- JDK8较JDK7部分新特性
- JDK7、JDK8新特性
- jdk7、jdk8新特性例子
- jdk7和jdk8的一些新特性
- JDK7与JDK8的新特性
- jdk7和jdk8的一些新特性
- jdk8新特性(附jdk5/jdk6/jdk7新特性)
- JDK8新特性(部分)
- 浅谈JDK8相对于JDK7的一些新特性
- 浅谈JDK8相对于JDK7的一些新特性
- JDK4.0 ,JDK5.0,JDK6.0,JDK7.0,JDK8.0的新特性
- JDK7新特性
- JDK7 新特性
- JDK7 新特性
- Jdk7新特性
- JDK7新特性笔记
- JDK7新特性
- JDK7新特性
- matlab面向对象编程基本
- 备忘录
- 3373-数据结构实验之查找一:二叉排序树
- 【剑指offer】面试题39 数组中出现次数超过一半的数字
- GC面试心得
- JDK8较JDK7部分新特性
- windows下git使用
- 3374-数据结构实验之查找二:平衡二叉树
- Servlet.service() for servlet [springServlet] in context with path [/TESTVIDEO] threw exception [Req
- 使用poco库搭建简单http服务器实现hello world
- 3375-数据结构实验之查找三:树的种类统计
- 一致性哈希算法
- Java中的锁分类
- 3642-判断给定森林中有多少棵树特别版