Differences and usage of string, StringBuffer and StringBuilder in Java

1 Introduction

In the Java language, there are eight basic data types: byte, short, int, long, float, double, Boolean and char. Char type is used to represent a single character, such as a, B, C, a, B, C, & these case letters or special characters. In actual programming, a single character is not used as frequently as we think. On the contrary, the "string" composed of multiple characters is more common, but there is no string in the basic data type. In order to solve this problem, the Java language provides us with a class string modified by the final keyword, which represents the string type. At the same time, because it is modified by final, it also shows that we can only use this class to create strings and call methods, but we can't inherit it.

Although the Java language provides us with the string class string, which makes it convenient for us to use the string type. Let's say so, in the continuous development, we find that the simple string type is not enough to meet our needs! Therefore, based on the string type, two string builders, StringBuffer and StringBuilder, are derived. For these three types, we need to face them often, whether in the process of job interview or in the actual work in the future, so it is particularly important to understand the differences between them!

2 String

By reading "1 Introduction", we have a general understanding of string types! It is said that it is a data type, but it is not a basic data type, but a non inheritable class modified by final, which is located in Java Lang package. As for how to use string type, there are two methods: direct assignment and new creation. Specific examples are as follows:

In common string operations, it is particularly common to judge whether two strings are equal, and there are two common discrimination methods, namely, using the methods equals and = = operators provided by the string class. In the blog "the difference between equals and = = in Java language", the author details the difference between the two, and interested children's shoes can refer to it. Next, the author will introduce some frequently used API methods of string class:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>