Java new features between 5 and 8

本文详细介绍了从Java5到Java8的重要新特性,包括for-each语法、@override注解、可变参数列表、枚举类型、协变返回类型、Queue和Iterable接口的增强、Lambda表达式、类型推断改进以及函数式接口的引入。特别强调了Java8中接口的默认方法和静态方法、lambda表达式的简洁语法和功能接口的概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java 5 

1. for-each syntax

2. @override was introduced in Java 5, modified in Java 7

add this annotation and the compiler will produce an error message if you accidentally overload instead of overriding.

3. Variable-Length Argument Lists: a variable-length argument is specified by three periods(…). For Example,

public static void m(int ... varargs) 
{
   // method body
} 

4. enum keyword

An especially nice feature is the way that enums can be used inside switch statements. 

enum classes cannot have finalize methods. enum can't deserialize enum.

5. covariant return types

details see example.

6. add Queue.

7. add Iterable Interface.

Java 8

1. forEach() which is heavily used.

(the Java compiler produces its own “assembly code,” but this code is run by the Java Virtual Machine rather than directly on a hardware CPU).

2. With Java 8 the interface waters have been muddied a bit, because Java 8 allows both default methods and static methods.

The default keyword formerly used only in switch statements and annotations. Details see here.

3. adds the ability to include static methods inside interfaces.

4. lambda expressions

Before Java 8, the only way to produce closure-like behavior was through inner classes. With Java 8, we now have lambda expressions which also have closure behavior, but with much nicer and more concise syntax. Details see here.

5. Before Java 7(include 7), we had to, in effect, duplicate the type declaration on both sides, like this:

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

, in Java 8, we would use like this:

 

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

details see here

6. functional interface

to be added later ......

references:

1. http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/lang/Enum.java 

2. http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/function

3. https://howtodoinjava.com/java8/functional-interface-tutorial/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值