[JAVA] About the StringBuilder class


I didn't know how to concatenate strings in the StringBuilder class

I tried to summarize this time to fix the knowledge.


First of all, the String class is a ** Immutable ** (immutable) class.

Therefore, every time a character string is concatenated with the + operator, the area in memory becomes larger.

It does not spread and make the String instance larger.

The mechanism is that every time you add or change a string

A new String class has been generated.

In other words, because it is instantiated many times internally

It puts a load on the JVM and slows down the process.

*** Character string concatenation using "+" wastes memory. *** ***

A class that solves this problem and can concatenate strings without delay

StringBuilder class </ strong> and StringBuffer class </ strong>.

These classes are ** mutable ** (variable) classes.

Character concatenation by append () method becomes character string concatenation by "+"

Compared to this, it enables very efficient processing.

This time I will summarize StringBuilder </ strong>.

Sample code

The flow is as follows.

  1. Call the append () method with a value in the actual argument Add strings to the buffer.

  2. Finally, call the toString () method Extract the completed character string.

Digression: The buffer has the meaning of buffering. It also has the meaning of a storage area.

Sample code


public class Main {

	public static void main(String[] args) {

		 *Instantiate the StringBuilder class
		 *It has a capacity (buffer) for 16 characters by default.
		StringBuilder sb = new StringBuilder();

		//① append()Call a method and add a string to the buffer
		sb.append("White rice,");

		sb.append("miso soup,");

		sb.append("Fried egg,");


		//② toString()Call and retrieve the string.
		String morning = sb.toString();



Output result


White rice,miso soup,Fried egg,Natto

Method chain

Also, the append () method has a special return value.

See API documentation.java

//Method declaration
public StringBuilder append(String str)

-Explanation of return value: Reference to this object

Even if the return value is void just to add a string to the buffer

I don't mind, but I dare to use "myself" as the return value

The reason for returning is to enable the following names.


StringBuilder sb = new StringBuilder();
sb.append("White rice,").append("ramen,").append("Gyoza");

In this way, a series of methods that return a reference to itself

The method of calling is called *** method chain ***.

at the end

Until now, string concatenation has been done with the + operator.

I was a little confused (Zub's amateur bare ... lol).

But if using this class will greatly improve the processing

I want to be able to use it as soon as possible! !!

Really, "Get used to it rather than learn!" Lol


[Introduction to Java Practical Edition](https://www.amazon.co.jp/dp/4844336770/ref=asc_df_48443367702547157/?tag=jpgo-22&creative=9303&creativeASIN=4844336770&linkCode=df0&hvadid=295686767484&hvpos=1o1&hvnetw=g&h4 & hvqmt = & hvdev = c & hvdvcmdl = & hvlocint = & hvlocphy = 10288852 & hvtargid = pla-524383774470 & th = 1 & psc = 1)

  • When purchasing the above book, check the page behind the book to see if it is the ○ print. Be sure to buy the latest version!

Recommended Posts