18
votes

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.

1
No, the questions has an error, while mine just finished normally but returns nothing. Also my number isn't nearly as big and still withing int range 8+8+1+3 = 20 bit length. - HopefullyHelpful
its perfectly works just check out here > ideone.com/nE6vEB - Vikrant Kashyap

1 Answers

33
votes

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).