String Builder

Introduction

String Builder is a separate class just like String, which allows us to modify the values(mutability).

Need of String Builder

As we know that Strings are immutable ,because of which
each and every time we need to cr…


This content originally appeared on DEV Community and was authored by Asswin CR

Introduction

String Builder is a separate class just like String, which allows us to modify the values(mutability).

Need of String Builder

As we know that Strings are immutable ,because of which
each and every time we need to create an object and that object will have no reference in later cases while running the loop , hence there will be huge memory wastage and due to this we use String Builder.

Code

public class SB {
    public static void main(String[] args) {
        StringBuilder builder=new StringBuilder();
        for(int i = 0;i<26;i++){
            char ch=(char) ('a'+i);
            builder.append(ch);
        }
        System.out.println(builder.toString());
    }
}

Code Explanation:

  • This is a simple code which demonstrates the use of String Builder class to print all alphabets from a-z, and to show how efficient it is in space and time complexity.
  • So inside the main function, we create a StringBuilder using "new" keyword, just like how we use to create for Strings.
  • Now inside the for loop we take ASCII value of a and add it to the iterating variable i and finally typecast the ASCII value to a character and assign it to ch . * In next step we add( append ) each character to the builder and finally print it after converting them to String format by calling toString() Method.

Code Analysis

1.Now if we analyze the Time Complexity of using Strings for the above example:

[a,ab,abc,abcd,...........,abcdefghijklmnopqrstuvwxy]

  • Here, Size of each items are 1+2+3+4...+26 (N). This can be represented as N(N+1) /2 .

  • After eliminating constants and less dominating terms , we finally obtain O(N^2).

  • All of these large Strings will have no reference variable [Wastage of Space].

  1. #### Now if we analyze the Time Complexity of using String Builder for the above example:
  • Here instead of creating new object every time , it makes changes in the exsiting object itself[like how Arrays does], hence saving memory space and time. For example:

["abc"+d -> "abcd"]

  • Due to this the Time Complexity is O(N).


This content originally appeared on DEV Community and was authored by Asswin CR


Print Share Comment Cite Upload Translate Updates
APA

Asswin CR | Sciencx (2021-10-04T14:54:55+00:00) String Builder. Retrieved from https://www.scien.cx/2021/10/04/string-builder/

MLA
" » String Builder." Asswin CR | Sciencx - Monday October 4, 2021, https://www.scien.cx/2021/10/04/string-builder/
HARVARD
Asswin CR | Sciencx Monday October 4, 2021 » String Builder., viewed ,<https://www.scien.cx/2021/10/04/string-builder/>
VANCOUVER
Asswin CR | Sciencx - » String Builder. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/04/string-builder/
CHICAGO
" » String Builder." Asswin CR | Sciencx - Accessed . https://www.scien.cx/2021/10/04/string-builder/
IEEE
" » String Builder." Asswin CR | Sciencx [Online]. Available: https://www.scien.cx/2021/10/04/string-builder/. [Accessed: ]
rf:citation
» String Builder | Asswin CR | Sciencx | https://www.scien.cx/2021/10/04/string-builder/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.