RFR: 8316187: Modernize an example in StringTokenizer

Pavel Rappo prappo at openjdk.org
Fri Sep 15 08:31:38 UTC 2023


On Wed, 13 Sep 2023 22:55:00 GMT, Naoto Sato <naoto at openjdk.org> wrote:

>> This modernizes an example to use the extended for-statement introduced in JDK 1.5.
>> 
>> I understand that StringTokenizer is a legacy class. But legacy or not, a class shouldn't promote older constructs when newer fit better. Especially when advising on preferred alternatives to itself.
>> 
>> That said, I wouldn't go as far as to use `var` anywhere in that example: JDK 10, which introduced `var`, might still be relatively new to some. Nor would I inline the call to `String.split` in the for-statement to dispense with the `String[] result` variable: I reckon it's good for a reader unfamiliar with `String.split` to see the type it returns.
>> 
>> Perhaps one additional thing to ponder is this: we could either add `@see` to point to `String.split` or make the whole example a `@snippet`, which `@link`s code to the definition of `String.split`.
>
> Marked as reviewed by naoto (Reviewer).

Thanks for your review, @naotoj. I found a few more similar cases in the related classes. Do you think I can add them to this PR, change the title appropriately, and get it re-reviewed, or would you prefer if I create a new PR?


diff --git a/src/java.base/share/classes/java/text/DateFormat.java b/src/java.base/share/classes/java/text/DateFormat.java
index ed6356e3fd8..e6760a6bca6 100644
--- a/src/java.base/share/classes/java/text/DateFormat.java
+++ b/src/java.base/share/classes/java/text/DateFormat.java
@@ -85,8 +85,8 @@
  * <blockquote>
  * {@snippet lang=java :
  * DateFormat df = DateFormat.getDateInstance();
- * for (int i = 0; i < myDate.length; ++i) {
- *     output.println(df.format(myDate[i]) + "; ");
+ * for (Date d : dates) {
+ *     output.println(df.format(d) + "; ");
  * }
  * }
  * </blockquote>
diff --git a/src/java.base/share/classes/java/text/NumberFormat.java b/src/java.base/share/classes/java/text/NumberFormat.java
index 4628870bbdb..ff369ee6682 100644
--- a/src/java.base/share/classes/java/text/NumberFormat.java
+++ b/src/java.base/share/classes/java/text/NumberFormat.java
@@ -82,8 +82,8 @@
  * <blockquote>
  * {@snippet lang=java :
  * NumberFormat nf = NumberFormat.getInstance();
- * for (int i = 0; i < myNumber.length; ++i) {
- *     output.println(nf.format(myNumber[i]) + "; ");
+ * for (var n : numbers) {
+ *     output.println(nf.format(n) + "; ");
  * }
  * }
  * </blockquote>

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

PR Comment: https://git.openjdk.org/jdk/pull/15716#issuecomment-1720884886


More information about the core-libs-dev mailing list