18

The following returns nothing for me in eclipse, is this expected behaviour ?

StringBuilder sb = new StringBuilder("");
for(int i = 0; i < 256*256*2*6; i++) {
   sb.append("a");
}
System.out.println(sb.toString());

The code returns without error and the size is well withhin memory capacity unlike in Maximum number of characters stringbuilder can accommodate

Solved: The problem lies in the eclipse console and enabling (Window -> Preferences -> Run/Debug -> Console -> fixed width console) will print the line normally.

Community
  • 1
  • 1
HopefullyHelpful
  • 1,652
  • 3
  • 21
  • 37

1 Answers1

33

The problem is probably your console being unable to print such a long string. If you try:

System.out.println(sb.toString().length());

instead, it will print what you expect (786432).

assylias
  • 321,522
  • 82
  • 660
  • 783
  • 1
    I tried in eclipse, indeed it doesn't work, even though the console buffer is large enough. It seems more related to the length of the _line_, because appending `a\n` instead of `a`, will display the whole content in my case. – Arnaud May 03 '16 at 09:00
  • @Berger Do you have the *Limit Console Output* unchecked in Eclipse (Window -> Preferences -> Run/Debug -> Console)? Btw, the max for `System.out.println` is equal to `Integer.MAX_VALUE` (2,147,483,647) if I'm not mistaken, this is the max an array can hold and a String is a char[]. – Kevin Cruijssen May 03 '16 at 09:01
  • @Kevin Cruijssen : I disabled the limit, but the huge line didn't display . – Arnaud May 03 '16 at 09:02
  • 4
    Enabling (Window -> Preferences -> Run/Debug -> Console->fixed width console) will print the line normally, and when choosing a maximum character width an error is displayed, bust be withing 80 and 1000 inclusive. – HopefullyHelpful May 03 '16 at 09:03
  • @HopefullyHelpful : You're right, changing this setting immediately made the invisible line display correctly. – Arnaud May 03 '16 at 09:04
  • Additionally changing the maximum character width to anything other than 80 throws an eclipse error message at me, 'Reveal End of document' has enocuntered a problem an internal error has occured. OK || Details An Internal error has occured. [random number] – HopefullyHelpful May 03 '16 at 09:07