search
HomeJavajavaTutorialRegaining the basics of Java (13): Summary of String sisters StringBuffer and StringBuilder

Regain the basics of java (13): Summary of String sisters StringBuffer and StringBuilder

1. Overview of the StringBuffer class

  1. buffer: Buffer

2. String buffer, very similar to String, is used to store string data

3. Both the String class and the StringBuffer class have a char array, which is the buffer. We cannot operate the buffer of the String class, but the buffer of the StringBuffer class can control its length.

4 .java.lang package

5. When the buffer is not enough, it can automatically grow

2. Construction method

public StringBuffer(),无参构造方法,构造一个空的字符串缓冲区,初始容量为16个字符
public StringBuffer(int capacity),构造一个指定容量的空的字符串缓冲区
public StringBuffer(String str),使用一个字符串作为初始内容来构造一个字符串缓冲区,并在后面预留16个字符的空缓冲区
1. StringBuffer sb="hello";   不行
2. StringBuffer sb=new StringBuffer("hello");   
sb+"world";   不行

3. Functional method

public StringBuffer append(任意类型 o),把任意类型数据的字符串表达形式追加到缓冲区的最后(例如:如果是对象,追加的是其toString方法的返回值)
public StringBuffer insert(int offset,任意类型 o),把任意类型的字符串表达形式插入到缓冲区指定位置
public int capacity(),获得字符串缓冲区的当前容量
public int length(),获得字符串缓冲区内字符串的长度
public StringBuffer delete(int start,int end),删除缓冲区指定起始位置的字符串
public StringBuffer deleteCharAt(int index),删除缓冲指定位置的字符
public StringBuffer replace(int start,int end,String str),把缓冲区指定位置的字符串替换为新的字符串
public StringBuffer reverse(),字符串反转(倒)
public String toString(),把StringBuffer转换为String类型

4. The difference between String and StringBuffer

1. StringBuffer sb="hello";   不行
2. StringBuffer sb=new StringBuffer("hello");   sb+"world";   不行
3. String对象是不可变的,StringBuffer对象是可变的(画内存分配图)
4. StringBuffer保证线程安全(数据同步),String不保证线程安全(数据不同步)

5. StringBuilder class

  1. It is the same as StringBuffer, the two classes are compatible

2. This class does not guarantee thread safety

3. Without considering multi-threading, characters The efficiency of the three string sisters: StringBuilder class> StringBuffer class> String class If you connect string constants, it is more efficient to use the "+" of the string; if you connect string variables, it is more efficient to use the append method of StringBuffer

6. Packaging class

  1. ##Java has 8 basic data types: byte, short, int, long;float, double;char, boolean

2. String s="100"; String s="99.999" ;

3. Java provides reference data types corresponding to basic data types: Byte, Character, Short, Integer, Long, Float, Double, Boolean

4. Whether they are reference data types or basic data types, their functions are the same. The main difference is: reference types can provide methods, but basic data types cannot.

5. The reference data type corresponding to the basic data type is called a wrapper class

6. Usage of wrapper class Integer i=new Integer(100); //Boxing //int i=100; Integer j=100; //Automatic boxing (after JDK5) System.out.println(i.intValue()+100); //Unboxing System .out.println(i+100); //Automatic unboxing (after JDK5)

7. The main functions of packaging classes

These classes provide a few Functional method, you can convert String type data into a wrapper class or basic data type 1. Convert between String and wrapper class a. Convert wrapper class to String Integer i=100; i+"" toString() b.String conversion For the wrapper class valueOf: Which class this method is in, then its role is to convert other types into the class it is in 2. Mutual conversion between String and basic data types a. Convert basic data types to String + "" b .String converted to basic data type


8. Regular expression

  1. The program needs to verify the data entered by the customer

2. The role of regular expressions is to verify data (for format)

3. Example: Zhengzhou fixed phone number (0371-56061160-223) Area code Fixed to 0371. The phone number is fixed to 8 digits and cannot start with 0. The extension number is optional, 1-3 digits are all numbers. Use "-" to separate

4. String zz_phone="0371-[1- 9]//d{7}(-//d{1,3})?";

5. Regular expressions exist in the form of strings

6. Regular expressions An expression is composed of a bunch of special symbols, used to describe or express the format of a certain data

9. How to write regular expressions

1. First write the fixed 2. Then write it in blocks. Each block first specifies the type and then the number


10. How to use regular expressions for data verification

1.String类的public boolean matches(String regex){}String email="601141632@qq.com";String email_regex=".+@//w+//.[a-z]{2,}";email.matches(email_regex);
2.java.util.regex包中有个Pattern类public static matches(String regex,String input){}

11.

  1. The split method of String class supports regular expressions

2 The replace method of the .String class does not support regular expressions, but replaceAll supports regular expressions


The above is to regain the basics of java (13): String The content summarized by sisters StringBuffer and StringBuilder. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
ZipInputStream failed to decompress Chinese file name? How to set the character set correctly?ZipInputStream failed to decompress Chinese file name? How to set the character set correctly?Apr 19, 2025 pm 04:33 PM

Discussion on ZipInputStream character set settings Many developers use ZipInputStream to decompress zip compressed packages containing Chinese file names or folder names, �...

How to implement a retry strategy from serverB to serverC using Spring WebFlux when building LLM gateway?How to implement a retry strategy from serverB to serverC using Spring WebFlux when building LLM gateway?Apr 19, 2025 pm 04:30 PM

Implementing the retry strategy using SpringWebFlux in building an LLM...

How to ensure that @Scheduled timing tasks are executed only once in Spring Boot multi-node environment?How to ensure that @Scheduled timing tasks are executed only once in Spring Boot multi-node environment?Apr 19, 2025 pm 04:21 PM

How to avoid repeated execution of timed tasks in SpringBoot multi-node environment? In Spring...

In object-oriented programming: Are attributes and states really equivalent?In object-oriented programming: Are attributes and states really equivalent?Apr 19, 2025 pm 04:18 PM

Deeply discussing properties and states in object-oriented programming. In object-oriented programming, the concepts of properties and state are often confused, and there is a subtle between them...

How to deal with a number overflow error when connecting to Oracle database in IDEA?How to deal with a number overflow error when connecting to Oracle database in IDEA?Apr 19, 2025 pm 04:15 PM

How to deal with digital overflow errors when connecting to Oracle database in IDEA When we are using IntelliJ...

How to use @ResultType annotation correctly in MyBatis?How to use @ResultType annotation correctly in MyBatis?Apr 19, 2025 pm 04:12 PM

When studying the MyBatis framework, developers often encounter various problems about annotations. One of the common questions is how to use the @ResultType annotation correctly...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool