What does String.repeat(int) do, and what are its edge cases and uses?
answer
- repeat(n) = n concatenated copies (Java 11)
- 0 -> "", 1 -> same string
- negative -> IllegalArgumentException
- overflow -> OutOfMemoryError
- replaces StringBuilder loop / nCopies+join trick
basics
~10 srepeat(n) (Java 11) returns a new string formed by concatenating this string n times. "ab".repeat(3) is "ababab". repeat(0) gives "", and a negative count throws IllegalArgumentException.
solid answer
~40 sString.repeat(int count), added in Java 11, returns a new String consisting of this string concatenated count times. "=".repeat(20) builds a 20-character line of equals signs. Edge cases: repeat(0) returns the empty string; repeat(1) returns a string equal to the original; a negative count throws IllegalArgumentException; and a count large enough to overflow the maximum array/string size throws OutOfMemoryError. It replaces older idioms like building a loop with StringBuilder or the Collections.nCopies + String.join trick, and is both clearer and efficient internally. Common uses: ASCII separators/banners, padding, indentation, and test fixtures. Because Strings are immutable, the original is unchanged and a new instance is returned.
go deeper
Knows repeat(n) produces n copies of the string concatenated together.
Knows the edge cases: 0 -> empty, negative -> IllegalArgumentException, and that it returns a new immutable string.
Explains the OutOfMemoryError bound from int-length arrays and that repeat replaces the older nCopies/StringBuilder idioms with a more efficient single allocation.
Weighs repeat in hot paths (single allocation vs. repeated growth), guards against attacker-controlled counts causing OOM, and standardizes its use for formatting helpers.
## What it does `String.repeat(int count)` (Java 11) returns a new `String` made by joining the original string to itself `count` times, with nothing in between. Examples: - `"ab".repeat(3)` -> `"ababab"` - `"-".repeat(10)` -> `"----------"` - `"x".repeat(1)` -> `"x"` ## Edge cases (the interview points) 1. **count == 0** -> returns the empty string `""`. 2. **count == 1** -> returns a string equal to the original (the spec allows returning the same instance). 3. **count < 0** -> throws `IllegalArgumentException` (you cannot repeat a negative number of times). 4. **Huge count** -> if the resulting length would exceed the maximum size of a Java array/String, it throws `OutOfMemoryError`. Strings are backed by an array whose length is an `int`, so there is an upper bound near `Integer.MAX_VALUE`. 5. **Empty receiver** -> `"".repeat(n)` is `""` for any valid n (joining empty strings yields empty). ## Why it was added Before Java 11 there was no direct way to repeat a string. People wrote: - a manual loop appending to a `StringBuilder`, or - the clever but obscure `String.join("", Collections.nCopies(n, s))`. Both are verbose; `repeat` is a single, readable call and is implemented efficiently inside the JDK (it allocates the target once rather than growing repeatedly). ## Immutability note Like all String methods, `repeat` does not modify the original (Strings are immutable). It returns a brand-new String. ## Common uses - Drawing separators/banners in console output: `"=".repeat(40)`. - Building padding or indentation: `" ".repeat(depth * 2)`. - Generating test data of a known length. ## Deriving the answer at any level Facts: repeat(n) concatenates n copies; 0 -> empty, 1 -> same, negative -> IllegalArgumentException, overflow -> OutOfMemoryError; immutable return; replaces StringBuilder loop / nCopies trick. From these you can answer at any depth.
- What does "x".repeat(-2) do?It throws IllegalArgumentException, because the count must be non-negative.
- How did people repeat a string before Java 11?With a StringBuilder loop, or String.join("", Collections.nCopies(n, s)). repeat() replaces both.
saying these in an interview costs you the question
- Saying repeat(-1) returns "" instead of throwing
- Claiming it mutates the original string
- Thinking repeat(0) throws rather than returning ""
- Not knowing it exists and reaching for a manual loop in modern code