In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Today, I will talk to you about the principles and common methods of JavaStringBuilder, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
The Origin of StringBuilder Class
Because the object content of the String class is immutable (the underlying is an array decorated by final), every time we do string concatenation, we always create a new object in memory. If you concatenate a string, each concatenation will build a new String object, which is both time-consuming and space-consuming. To solve this problem, we can use the java.lang.StringBuilder class.
A brief introduction to StringBuilder
The API,StringBuilder that looks up java.lang.StringBuilder, also known as a variable character sequence, is a string buffer similar to String, and the length and content of the sequence can be changed by some method calls. It turns out that StringBuilder is a buffer of strings, that is, it is a container that can hold many strings. And can perform various operations on the strings in it. It has an array inside to store the contents of the string, and when the string is concatenated, the new content is added directly to the array. StringBuilder automatically maintains the expansion of the array (the default is 16 characters, which exceeds the automatic expansion).
There are two common construction methods:
Public StringBuilder (): construct an empty StringBuilder container.
Public StringBuilder (String str): construct a StringBuilder container and add a string to it.
There are two common methods for StringBuilder:
Public StringBuilder append (...): adds a string of any type of data and returns the current object itself. Any data as a parameter adds the corresponding string contents to the StringBuilder.
Package demo06;public class Demo01StringBuilder {public static void main (String [] args) {/ / create StringBuilder object StringBuilder bu = new StringBuilder (); / / add data to StringBuilder using the append method, you can add any type of data / * chained programming: the return value of the method is an object and you can continue to call method * / bu.append ("abc"). Append (1) .append (true) .append (8.8) .append ('medium') System.out.println (bu); / / abc1true8.8}}
Public String toString () `: converts the current StringBuilder object to a String object.
Package demo06;public class Demo02StringBuilder {/ * StringBuilder and String can be converted to each other: String- > StringBuilder: you can use StringBuilder's constructor StringBuilder (String str) to construct a string generator and initialize it to the specified string content. StringBuilder- > String: you can use the toString method public String toString () in StringBuilder to convert the current StringBuilder object to a String object. * / public static void main (String [] args) {/ / String- > StringBuilder String str = "hello"; StringBuilder bu = new StringBuilder (str); / / add data bu.append ("world") to StringBuilder; System.out.println ("StringBuilder:" + bu); / / StringBuilder- > String String s = bu.toString (); System.out.println ("string:" + s);}}
The result after the code execution
After reading the above, do you have any further understanding of the principles and common methods of JavaStringBuilder class? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.