What is the concept of string in java

03-17-2023

This article mainly introduces the relevant knowledge about the concept of string in java. The content is detailed and easy to understand, the operation is simple and fast, and it has certain reference value. What we have gained, let's take a look at it together.


In java, string means string, a class type (class type), which represents a sequence of characters; Java has no built-in characters String type, but provides a String class in the standard Java class library to create and manipulate strings. The easiest way to define a string in Java is to surround it with double quotes; you can also define a string by creating an instance of the String class.

What is string in java

In Java, String is a class type (class type), which represents a string A sequence of characters, so we often refer to it as a string.

String is a special class in Java. It is used like a general basic data type and is widely used in Java programming. Java does not have a built-in string type, but instead provides a String class in the standard Java class library to create and manipulate strings.

The easiest way to define a string in Java is to enclose it in double quotes. This string of characters enclosed in double quotes is actually a String object, such as the string Hello becomes a String object after compilation. Strings can therefore also be defined by creating instances of the String class.

No matter which form is used to create a string, once the string object is created, its value cannot be changed, but it can be changed by reassigning other variables.

Note: Since the content of classes and objects will only be explained in Chapter 8, as long as you encounter the concepts of classes and objects, you only need to know how to operate them, and you don’t need to understand why. use.

Java defines strings (2 ways)

Directly defines strings

< p>Defining a string directly refers to using double quotes to represent the content in the string, such as Hello Java, Java programming, etc. The specific method is to directly initialize a String object with a string constant, as follows:

String str = "Hello Java";

or

String str; str = "Hello Java";


Note: String variables must be initialized before they can be used.

Example 1: The following example demonstrates several usages of creating strings directly.

String str = "I am a little bird"; // Result: I am a little bird String word; word = "I am a bird"; // Result: I am a bird word = "

to fly

"; // Result:

to fly

word = "Let\'s say that it\'s true"; // Result: Let's say that it's true System.out.println(word); word = "Beijing\\Shanghai\\Guangzhou"; // Result: Beijing\Shanghai\Guangzhou


use String class definition

Before we mentioned that each string defined by double quotes in Java is an object of the String class. Therefore, you can create a string by using the constructor of the String class, which is located in the java.lang package (about the packages commonly used in Java, we will explain in detail later in the tutorial).

The constructor of the String class has multiple overloaded forms, each of which can define a string. The most commonly used forms are described below.

Note: A method with the same name as the class name and no return type is called a constructor. Overloading refers to defining multiple methods with the same name in a class, but requires each method to have a different parameter type or number of parameters. It will be explained in detail later in the tutorial, so you can learn about it here.

1. String()

Initializes a newly created String object representing an empty character sequence.

2. String(String original)

Initializes a newly created String object to represent a sequence of characters identical to the parameter. In other words, the newly created string is a copy of the parameter string. For example:

String str1 = new String("Hello Java"); String str2= new String(str1);


here the values of str1 and str2 are equal.

3. String(char[ ]value)

Allocate a new string, and change all the character array elements in the parameter into strings. The contents of this character array have been copied, and subsequent modifications to the character array will not affect the newly created string. For example:

char a[] = {'H','e','l','l','0'}; String sChar = new String(a); a[1] = 's';


The value of the above sChar variable is the string Hello. Even after the string was created, the 2nd element in the a array was modified, but the value of sChar was not affected.

Note: If you don't know what an array is, you can read the "Introduction to Java Arrays" section to get a general understanding of arrays before continuing to study this section.

4. String(char[] value,int offset,int count)

Allocate a new String, which contains a parameter from the character array The character of the subarray. The offset parameter is the index of the first character of the subarray, and the count parameter specifies the length of the subarray. The content of this subarray has been assigned, and subsequent modifications to the character array will not affect the newly created string. For example:

char a[]={'H','e','l','l','o'}; String sChar = new String(a,1,4); a[1]='s';


The value of the above sChar variable is the string ello. This constructor uses some consecutive elements in the character array to create a string object. The offset parameter specifies the starting index value, and count specifies the number of intercepted elements. After the string object is created, even if the value of the second element in the a array is modified later, it has no effect on the value of sChar.


Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us