JDK-8192858 : Release Note: Bytecode Generation for Enhanced for Loop
  • Type: Sub-task
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 10
  • Priority: P3
  • Status: Closed
  • Resolution: Delivered
  • Submitted: 2017-11-30
  • Updated: 2018-04-03
  • Resolved: 2017-11-30
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 10
10Resolved
Description
Bytecode generation has been improved for enhanced for loops, providing an improvement in the translation approach for them. For example:
```
List<String> data = new ArrayList<>();
for (String b : data);
```
The following is the code generated after the enhancement:
```
{
    /*synthetic*/ Iterator i$ = data.iterator();
    for (; i$.hasNext(); ) {
        String b = (String)i$.next();
    }
    b = null;
    i$ = null;
}
```
Declaring the iterator variable outside of the for loop allows a null to be assigned to it as soon as it is no longer used. This makes it accessible to the GC, which can then get rid of the unused memory. Something similar is done for the case when the expression in the enhanced for loop is an array.
Comments
Edits per https://wiki.se.oracle.com/display/JPG/JPG+Release+Note+Style+Guide. Inc comments from the Release Notes review on 2/16 regarding RN titles and descriptions.
27-02-2018