RFR: 8197594 - String and character repeat

Jim Laskey james.laskey at oracle.com
Thu Feb 15 17:20:42 UTC 2018

This is a pre-CSR code review [1] for String repeat methods (Enhancement).

The proposal is to introduce four new methods;

1. public String repeat(final int count)
2. public static String repeat(final char ch, final int count)
3. public static String repeat(final int codepoint, final int count)
4. public static String repeat(final CharSequence seq, final int count)

For the sake of transparency, only 1 is necessary, 2-4 are convenience methods.
In the case of 2, “*”.repeat(10) performs as well as String.repeat(‘*’, 10).
3 and 4 convert to String before calling 1.

Performance runs with jmh (results as comment in [2]) show that these
methods are significantly faster that StringBuilder equivalents.
 - fewer memory allocations
 - fewer char to byte array conversions
 - faster pyramid replication vs O(N) copying

I left StringBuilder out of scope. It falls under the category of
Appendables#append with repeat. A much bigger project.

All comments welcome. Especially around the need for convenience
methods, the JavaDoc content and expanding the tests.

— Jim

[1] webrev: http://cr.openjdk.java.net//oj/home/jlaskey/8197594/webrev-00
[2] jbs: https://bugs.openjdk.java.net/browse/JDK-8197594

More information about the core-libs-dev mailing list