Java StringBuilder


Java provides us with an intuitive way of creating and working with sequence of characters that can be modified.

StringBulilder as class derives from AbstractStringBuilder.

public final class StringBuilder extends AbstractStringBuilder {}

It implements java.io.Serializable and java.lang.CharSequence.

public final class StringBuilder extends AbstractStringBuilder implements Serializable, CharSequence {}

StringBuilder resides in the java.lang package.

package java.lang;

Creating a StringBuilder.

StringBuilder provides several constructors to instantiate it.

No. Constructor Description
1. public StringBuilder() Instantiates a StringBuilder with an initial capacity of 16.
2. public StringBuilder(int capacity) Instantiates a StringBuilder with the specified capacity.
3. public StringBuilder(CharSequence charsequence) Instantiates a StringBuilder with the contents of the specified CharSequence.
4. public StringBuilder(String myString) Instantiates a StringBuilder with the contents of the specified String.

Example.

StringBuilder sb=new StringBuilder();

Appending Items to a StringBuilder

Items can be added to a StringBuilder object using the append() method.

sb.append("Mercury");
sb.append("Venus");
sb.append("Earth");

Chaining StringBuilder methods

Most of the methods in the StringBuilder return this so that the method calls can be chained together in a fluent manner.


sb.append("Mercury").append("Venus").append("Earth").append("Mars");

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post Next Post