Java — Need for StringBuffer and Important Constructors of StringBuffer Class | Code Factory

new capacity = (cc + 1) * 2
= (16 + 1) * 2 = 34
= (34 + 1) * 2 = 70
StringBuffer sb = new StringBuffer();
System.out.println(sb.capacity()); // 16
sb.append("abcdefghijklmnop");
System.out.println(sb.capacity()); // 16
sb.append("q");
System.out.println(sb.capacity()); // 34
StringBuffer sb = new StringBuffer(100);
System.out.println(sb.capacity()); //
/*
at 101th char capacity = (100 + 1) * 2
= 202
*/
StringBuffer sb = new StringBuffer("Code");
System.out.println(sb.capacity()); //
/*
at 101th char capacity = s.length() * 16
= 4 + 16
= 20
*/

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store