Home Java javaTutorial Regaining the basics of Java (13): Summary of String sisters StringBuffer and StringBuilder

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

Jan 16, 2017 am 10:08 AM

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";   不行
Copy after login

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类型
Copy after login

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不保证线程安全(数据不同步)
Copy after login

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){}
Copy after login

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 of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

See all articles