0

Below is the problem:

StringBuffer sbf = new StringBuffer();

for(var i=1;i<=1301;i++){     
   sbf.append(i);
}

System.out.println(sbf.toString());

printing empty but values are there inside

please see the values in below image

Img: Screenshot in debugmode

if we select stringbuffer object its shows empty but values are there you can observe

i am very strange to see this what is happening Can any one suggest solution for this

Bruno
  • 2,889
  • 1
  • 18
  • 25
  • StringBuilder is faster than StringBuffer, at the cost of not being synchronized. https://stackoverflow.com/questions/355089/difference-between-stringbuilder-and-stringbuffer?rq=1 – FailingCoder Nov 07 '19 at 16:21

1 Answers1

1

Notice how when the integer 10 is added, it is shown as 1, 0. My guess is your expectations are not met due to how different types are handled.

If you wanted a giant String, and added Strings it would make sense to you:

StringBuffer sbf = new StringBuffer();
sbf.append("10");
sbf.append("11");
System.out.println(sbf.toString());

This will output: "1011" as expected.

This is a common usage of StringBuffer.

When an integer is appended, a string representation is added. This is what is happening in your example. The integer 10 is converted to the string "10", add appended.

AndyMan
  • 397
  • 1
  • 7
  • if we append below or equal 1300 characters output is coming exactly as the same you said. But more than 1301 characters output showing empty and not assigned to String even values are there. – user8649278 Nov 08 '19 at 05:33
  • I got the solution for this we have to add between two elements TAB as separator. Note: should not use Space bar use TAB . it works fine . Really strange problem :) – user8649278 Nov 08 '19 at 06:47