Java 8 新特性
Java 8 新特性
Java 8 Features:
- Lambda expression
- Stream API
- Functional interface
- Default and static methods
- Optional class
- Nashorn – JavaScript runtime engine
- Annotation on Java Types
- Unsigned Integer Arithmetic
- Repeating annotations
- New Date and Time API
Unsigned Integer Arithmetic
answer from stackoverflow
Well, even in Java 8, long and int are still signed, only some methods treat them as if they were unsigned. If you want to write unsigned long literal like that, you can do
static long values = Long.parseUnsignedLong("18446744073709551615");
public static void main(String[] args) {
System.out.println(values); // -1
System.out.println(Long.toUnsignedString(values)); // 18446744073709551615
}
Stream API
pass
Lambda表达式和函数式接口
方法引用
更好的类型推断
pass
Date/Time API(JSR 310)
pass
Optional
pass
接口的默认方法和静态方法
pass
Java编译器 参数名称
Nashorn JavaScript引擎
Nashorn has been included with Java 8 through JDK 14
嗯是的,Jdk 15 就没有了。
Nashorn development continues on GitHub as a standalone OpenJDK project and the separate release can be used in Java project from Java 11 and up.
Base64 API
拓宽注解的应用场景
重复注解
Annotation on Java Types
import java.lang.annotation.*;
@Target(ElementType.TYPE_USE)
@interface TypeAnnoDemo {
}
public class MyClass {
public static void main(String[] args) {
@TypeAnnoDemo
String s = "Hello,I am annotated with a type annotation";
System.out.println(s);
myMethod();
}
static @TypeAnnoDemo int myMethod() {
System.out.println("There is a use of annotation with the return type of the function");
return 0;
}
}
Archives