java string overview and usage (basic)

Time:2022-8-5

java string overview and usage

String The String type is under the java.lang.String class package. Represents a string.

Overview:

The API documentation mentions that all string literals (like "abc") in java programs are implemented as instances of this class.

That is to say, all double-quoted strings in the program are objects of the String class, even if they are new.

Characteristics of strings:

1. The content of the string will never change.

2. It is precisely because the string cannot be changed, the string can be shared and used after it is established.

3. The effect of a string is equivalent to a char[] character array, but the underlying principle is a byte[] byte array.

Use: (create method)

Three constructors and one direct creation

Three construction methods:

1. public String (): Creates a blank string with no content

2. public String(char[] array), according to the content of the string array, to create the corresponding string.

3. public String(byte[] array), create a corresponding string according to the content of the byte array.

A direct creation: String str = "hello";(this is also a string character object)

Code example:

1. public String (): Creates a blank string with no content

public static void main(String[] args) {
        //construct with null parameters
        String str1 = new String();//Keep empty brackets
        System.out.println("The first string: " + str1);

2. public String(char[] array), according to the content of the string array, to create the corresponding string.

//create array from string
        char [] charArray = {'A','B','C'};
        String str2 = new String(charArray);
        System.out.println("The second string is: " + str2);

3. public String(byte[] array), create a corresponding string according to the content of the byte array.

//create string from byte array
        byte[] byteArray = {97,98,99};
        String str3 = new String(byteArray);
        System.out.println("The third string is: " + str3);

4. Create directly

//create directly
        String str4 = "hello";
        System.out.println("The fourth string is: " + str4);

Recommended Today

Node.js package manager NPM explained

The package manager, also known as the package management system, is a combination of tools for automatically installing, preparing, uninstalling and upgrading software packages in the computer. It is widely used in the installation and management of various system software and application software. It is also very beneficial to our business development, and the same […]