StringBuilder.toString() is printed as empty string in Eclipse-console when too big?

妖精的绣舞 提交于 2021-02-16 05:49:49

问题


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:


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



来源:https://stackoverflow.com/questions/36999661/stringbuilder-tostring-is-printed-as-empty-string-in-eclipse-console-when-too

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!