Introduction to Java Strings

1. String
A Java string is a sequence of Unicode characters, for example String name = "Nezha";

2. The subString method of substring
String can intercept a substring from a longer string, for example:

package com.nezha.javase;
 
public class Test {     public static void main(String[] args) {         String name = "CSDN Nezha";         System.out.println(name.substring(1));// SDN Nezha         System.out.println(name.substring(4,6));// Nezha         System.out.println(name.subSequence(4,6));// Nezha     } } name.substring A parameter indicates from the current Parameter interception, has been intercepted to the end.







When there are two parameters, the first is the starting position of the interception (inclusive), and the second is the ending position (exclusive).

What is the difference between substring and subSequence?

substring source code

public String substring(int beginIndex, int endIndex) {
    if (beginIndex < 0) {
        throw new StringIndexOutOfBoundsException(beginIndex);
    }
    if (endIndex > value.length) {
        throw new StringIndexOutOfBoundsException(endIndex);
    }
    int subLen = endIndex - beginIndex;
    if (subLen < 0) {
        throw new StringIndexOutOfBoundsException(subLen);
    }
    return ((beginIndex == 0) && (endIndex == value.length)) ? this
            : new String(value, beginIndex, subLen);
}
subSequence源码

public CharSequence subSequence(int beginIndex, int endIndex) {     return this.substring(beginIndex, endIndex); } subSequence also calls substring, but the return value is different.


The subSequence has been rewritten in the String class, and the subSequence method can be directly converted to a String object.

3. String splicing "+"
1. The principle of "+" connector The
string connection is realized through the StringBuilder (or StringBuffer) class and its append method, and the conversion of the object into a string is realized through the toString method. Defined by the Object class and inherited by all classes in Java.

We can verify it by decompiling:

public class Test {
    public static void main(String[] args) {
        int i = 10;
        String s = "哪吒";
        System.out.println(s + i);
    }
}
 
/**
 * 反编译后
 */
public class Test {
    public static void main(String args[]) { 
        ...
        byte byte0 = 10;      
        String s = "哪吒";      
        System.out.println((new StringBuilder()).append(s).append(byte0).toString());
    }
}

From the above code, it can be seen that the bottom layer of the "+" connection string is actually completed by the StringBuilder object through append and then calling toString.

2. Efficiency of the "+" connector
When using the "+" connector, the JVM will implicitly create a StringBuilder object. This method will not cause loss of efficiency in most cases. However, string splicing is performed in a loop Time is different.

Because a large number of StringBuilder objects will be created in the heap memory, this is definitely not allowed, so at this time it is recommended to create a StringBuilder object outside the loop, and then call the append method inside the loop for manual splicing.

There is also a special case, if the "+" splicing is a string in the string constant, the compiler will optimize and directly splice the two string constants.

Therefore, the "+" connector is very efficient for direct addition of string constants, because its value is determined during compilation; but for indirect addition, the efficiency will become lower. It is recommended to use StringBuilder for single-threaded. Use StringBuffer instead when multithreading.

[Java Basics 8] String, StringBuilder, StringBuffer Detailed Explanation

4. Immutable Strings

String does not provide methods for modifying strings, so String is immutable in Java.
Shangxuetang brings a brand-new Java 300 set course to students! A must-have high-quality tutorial for self-study Java for zero-basic beginners in java_Hand-in-hand diagrams to learn Java, making learning a kind of enjoyment_哔哩哔哩_bilibili

Guess you like

Origin blog.csdn.net/hutubiancheng/article/details/126875681