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

  • If content is fixed, not changed frequently or not changed then go for String.
  • If content is not fixed and keep on changing then never used String concepts, use StringBuffer.
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
*/
  • Constructs a string buffer initialized to the contents of the specified string. The initial capacity of the string buffer is 16 plus the length of the string argument.
StringBuffer sb = new StringBuffer("Code");
System.out.println(sb.capacity()); //
/*
at 101th char capacity = s.length() * 16
= 4 + 16
= 20
*/

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

3 Benefits of a SaaS Cloud ERP Platform for Mid-Market Organizations

Create an Elixir Phoenix app with asdf

Creating a Static “C” Library

Do You want to create your own OS (mission 9)

A Glimpse on OAuth

What Are .SVG Files?

Camel Idempotent Repository

New in Aiir: Updates to our Player

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
Code Factory

Code Factory

More from Medium

Spiral Matrix — Problem solution

Circuit Breakers: The Saviour at Lenskart

Migrating from Log4j1.x to Log4j2.x

Understanding Bubble Sort at grass root level — With examples that illustrate all edge cases