Java String repeat() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 11+

Definition

In Java, repeat(count) returns a new String made of the original text written count times in a row: "ab".repeat(3) is "ababab". A count of 0 gives the empty string.

It replaces the old loop-and-append pattern for things like separator lines, indentation, padding, and simple text charts, in one readable call.

repeat() is an instance method of java.lang.String, added in Java 11. Call it on a string, as in text.repeat(...); calling it on a null reference throws NullPointerException. Strings are immutable, so it never changes the original: it returns a new String (or the same one when nothing changes). Keep the result. Requires Java 11 or newer.

Syntax

Pass how many copies you want.

java
String line   = "-".repeat(20);
String indent = " ".repeat(level * 4);
CallWhat it does
"ab".repeat(2)"abab".
"=".repeat(5)"=====".
"x".repeat(0)"".

Repeating an empty string, or repeating once, simply gives back equal text. The result length is length() * count, so large counts can need a lot of memory.

Library declarations
java
public String repeat(int count);

Parameters

ParameterDescription
countHow many times to repeat; must be 0 or more.

Return Value

Return ValueDescription
StringThe text repeated count times, or "" for 0.

Java Examples

Example 1 — Repeat Text, Separators, and Nothing

Three counts: a few copies, a separator line, and zero.

java
public class RepeatBasic {
    public static void main(String[] args) {
        System.out.println("ab".repeat(3));
        System.out.println("-".repeat(10));
        System.out.println("[" + "x".repeat(0) + "]");
    }
}

How It Works

  1. Copies side by side. "ab".repeat(3) writes the two letters three times without any separator.
  2. Instant separator lines. A one-char string repeated 10 times draws a line of dashes.
  3. Zero copies. A count of 0 is legal and returns the empty string, shown as [].

Example 2 — Draw a Simple Bar Chart

Use each number as the repeat count of a bar.

java
public class RepeatBarChart {
    public static void main(String[] args) {
        String[] days = {"Mon", "Tue", "Wed"};
        int[] sales = {3, 7, 5};

        for (int i = 0; i < days.length; i++) {
            System.out.println(days[i] + " " + "#".repeat(sales[i]) + " " + sales[i]);
        }
    }
}

How It Works

  1. Parallel arrays. days[i] and sales[i] describe the same day.
  2. Count becomes length. "#".repeat(sales[i]) produces a bar whose length equals the sales figure.
  3. Readable at a glance. Tuesday's bar of 7 hashes is visibly the longest.

Example 3 — Right-Align Numbers and Reject a Negative Count

Pad each number with spaces to a width of six, then try a negative count.

java
public class RepeatAlign {
    public static void main(String[] args) {
        int[] numbers = {7, 42, 1234};
        for (int n : numbers) {
            String text = String.valueOf(n);
            System.out.println(" ".repeat(6 - text.length()) + text);
        }

        try {
            "a".repeat(-1);
        } catch (IllegalArgumentException e) {
            System.out.println("negative count: " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. Compute the padding. The width is 6, so 7 needs 5 spaces, 42 needs 4, and 1234 needs 2.
  2. Spaces in front. Prepending " ".repeat(...) lines up the last digits in one column.
  3. Negative counts fail. repeat(-1) throws IllegalArgumentException; make sure the width is never smaller than the text.

Frequently Asked Questions

It returns a new String containing the original text repeated count times, with nothing in between.
Java 11.
The empty string "".
repeat() throws IllegalArgumentException.
Use String.join("", Collections.nCopies(n, text)) or a StringBuilder loop.

Next: replace()

Replace every occurrence of a char or literal text.

replace() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful