RFR: 8274261: Use enhanced-for instead of plain 'for' in jdk.jcmd

Andrey Turbanov github.com+741251+turbanoff at openjdk.java.net
Fri Sep 24 09:04:56 UTC 2021


On Fri, 24 Sep 2021 08:45:54 GMT, Serguei Spitsyn <sspitsyn at openjdk.org> wrote:

>> There are few places in code where manual `for` loop is used with Iterator to iterate over Collection or Array.
>> Instead of manual `for` cycles it's preferred to use enhanced-for cycle instead: it's less verbose, makes code easier to read and it's less error-prone.
>> It doesn't have any performance impact: javac compiler generates similar code when compiling enhanced-for cycle.
>> 
>> One strange thing I also noticed is static field `sun.tools.jstat.Parser#reservedWords`, which filled in `Parser` constructor. Reworked to initialize it once.
>
> src/jdk.jcmd/share/classes/sun/tools/jstat/OptionFormat.java line 81:
> 
>> 79: 
>> 80:         for (Iterator<OptionFormat> i = children.iterator(); i.hasNext(); /* empty */) {
>> 81:             OptionFormat o = i.next();
> 
> Why did not you simplify the lines 80-81 the same way as in line 85?

It can't be simplified: it calls `Iterator.hasNext()` inside cycle body.
`i.hasNext()` at line 82

-------------

PR: https://git.openjdk.java.net/jdk/pull/5673


More information about the serviceability-dev mailing list