I want to print a lot of the same characters and I don't want to use a for loop and I'm also looking for a way to do it as less lines as possible. Even better I would like to be able to store that string in a static variable to use again and again.

This is how I currently do it (ugly but at least no for loop):

                + "----------------------------------------"
                + "----------------------------------------");

What's a better way (aesthetically wise)?

  • 2
    \$\begingroup\$ SO dupe : stackoverflow.com/a/1235184/7602 \$\endgroup\$
    – konijn
    Apr 23, 2014 at 16:05
  • \$\begingroup\$ @konijn didn't notice when I searched, sorry. Still though I think this is a more fitting question for this site now and it wouldn't be bad to have it. \$\endgroup\$
    – Aki K
    Apr 23, 2014 at 16:08
  • 1
    \$\begingroup\$ Rolfl made the question viable, I learned something ;) \$\endgroup\$
    – konijn
    Apr 23, 2014 at 16:14
  • 1
    \$\begingroup\$ In Python this is easily done with '-'*80, haha \$\endgroup\$
    – justhalf
    Apr 24, 2014 at 1:34

4 Answers 4


String constants in Java are very efficient. In practice, your code is very high-performing. The issue here is not about how fast the code runs, because, essentially, it cannot be run any faster than this.

If you want to have more efficient code in terms of space, then the practical thing to do is to declare the value as a string constant, something like:

private static final String DASHES = 
            + "----------------------------------------"
            + "----------------------------------------";

And then, in your code, you just:


This will be just as fast, but the code is easier to read.

If the one place where you declare the dashes is still unsightly, then you can compute the value using the other mechanisms shown in other answers... e.g. :

public static final String DASHES = new String(new char[80]).replace("\0", "-");

but that is overkill, and unnecessary.


In your comments you raised the issue of having multiple different-length values. I would recommend that you create a constant for the longest one (whether you use the String-constant or some other generator method), and then do a DASHES.substring(0, length) on that to get the shorter constants...

alternatively, I would consider a helper function as useful for this problem....

private static final String repeatChar(char c, int length) {
    char[] data = new char[length];
    Arrays.fill(data, c);
    return new String(data);

and then your constants can be initialized with:

private static final String DASH80 = repeatChar('-', 80);
private static final String SPACE40 = repeatChar(' ', 40);


  • \$\begingroup\$ I would argue though that since performance is of no concern (initializing it just once is really no performance hit at all) the second way is better since it is literally one line (makes your code way more concise comparing to 4 lines) and also it is way easier to fine tune the number of characters one could want. \$\endgroup\$
    – Aki K
    Apr 23, 2014 at 16:31
  • 3
    \$\begingroup\$ new String(new char[80]).replace("\0", "-"); now if just something like that would exist in Brainf*ck... \$\endgroup\$ Apr 23, 2014 at 16:40
  • \$\begingroup\$ @SilliconTouch I would agree with the performance statement (the static initializer is not a performance problem). The real question is whether a string constant is easier to read than the alternatives. Importing a third-party library for a static initializer seems overkill, and the new String(...).replace(...) in itself is complicated too. I am on the fence. But, you should choose whichever is more readable to you. Given that you started with the constant string, it seems to me that is more maintainable for you. \$\endgroup\$
    – rolfl
    Apr 23, 2014 at 16:40
  • 1
    \$\begingroup\$ ...and wish you were using C++, so the "concise" version could actually be somewhat concise, like: std::string DASHES(80, '-'); :-) \$\endgroup\$ Apr 23, 2014 at 16:43
  • \$\begingroup\$ @rolfl It is true that it would not be worth to import a third-party library just for this reason but in my situation I am already using the library in that class so it is not an issue. I think I will prefer the second way because in my situation I am still fine tuning how many dashes I would need so it would be easier to use and for some reason it looks kinda easier to my eyes. Anyway though your answer was really helpful and made me realize things I did not see. \$\endgroup\$
    – Aki K
    Apr 23, 2014 at 16:46

I wouldn't call this ugly:

                 + "----------------------------------------"
                 + "----------------------------------------");

There's nothing wrong with it. It's perfectly clear what it does and it's efficient.

If you print many lines like this then put the string in a variable so you can easily print as many times as you want:

private static final String DASHES = "----------------------------------------"
                                   + "----------------------------------------"
                                   + "----------------------------------------";

Maybe it's a hassle to create the string (for example, type "-" 10 times and copy and paste that 8 times) but you only have to do it once.

If you really want to do it programmatically, then this seems a relatively nice way (without external libraries):

public static final String DASHES = new String(new char[80]).replace("\0", "-");

Or if you don't mind using StringUtils (from commons-lang) then you might fancy this:

public static final String DASHES = StringUtils.repeat("-", 80);

But like @rolfl, I don't think it's worth the effort to memorize such trickery. You can always just type the thing, there's nothing wrong with it. Also keep in mind Occam's razor: the simplest solution is often the best.

See also this question on Stack Overflow:


  • \$\begingroup\$ I much prefer seeing StringUtils.repeat("-", 80), which more clearly and verifiably says: 80 hyphens/dashes. I've maintained projects with constants of 80 spaces that turned out not to be 80 spaces. Ne'er again. \$\endgroup\$
    – JvR
    Apr 24, 2014 at 11:12

Upvoted answers from What is the easiest way to generate a String of n repeated characters? are ...

int n = 80;
char[] chars = new char[n];
Arrays.fill(chars, '-');
String result = new String(chars);

... and ...

If you can, use StringUtils from Apache Commons Lang:

StringUtils.repeat("ab", 3);  //"ababab"

à la Java 8 :

Say you have a

StringBuilder str = new StringBuilder(80);
Stream.generate(() -> '-').limit(80).forEach(str::append);
  • 1
    \$\begingroup\$ OK, you've provided an alternative Code. Now, please add a Review, where you explain why your code is better than their code. \$\endgroup\$
    – mdfst13
    Feb 6, 2017 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.