String Class - String Types - Top Half Explanation - Java - Detail Magic

Preface


Character string:
There is no string type in C!
However, in Java and C++, there is a string type [String]

What is a string? What is a character?


Use double quotation marks, and double quotation marks containing any number of characters ("abcdef", "a") are strings.
Use single quotation marks, and a single quotation mark containing only one character ['a','strong'] is a character.

Attention:


1. In Java, there is no so-called string that ends with'\0'.
2. String class, cannot inherit.

Create String


Creating is almost the same as creating arrays.

First: direct assignment

Second: Call the construction method to construct the object (that is, remove the new'one)

Third:

public class Test {
    public static void main(String[] args) {
        char[] chars = {'a','b','c'};
        String str3 = new String(chars);
        System.out.println(str3);

    }
}

Understanding string types


Hold down Ctrl, click String, enter String


From the figure above, we find that for a string, there are two properties, one is a value array of type char (in this case, the array is just a variable [reference type], no memory is allocated to the array. There is no new). One is a hash code.


Let's start with a bottom line, and then I'll explain it in more detail.
Let's start by understanding String's objects (through the third method of creating strings)
String's common construction is arrays

Matters needing attention

String literal value constants such as "hello" are also of type String.
String is also a reference type. String str = "Hello"; This code memory layout is as follows

^**

Example 1

public class Test {
    public static void main(String[] args) {
        String str = "abcef";
        String str2 = str;
        System.out.println(str);
        System.out.println(str2);
    }
}

Design sketch

Appendix

Example 2: Re-assigning str in example 1 will affect the output of str 2?

In the two examples above, we need to understand that string constants cannot be changed

For example: String str = "abcd"; Modify the string "abcd" to "gbcd" by referencing str.
The answer is impossible, because double quotation marks cause literal value constants, which cannot be modified.
In Example 2, STR = author; This code redirects the STR to a new object (modifying the direction of the str) instead of changing the original string object to author.

Let's take a deeper look at why strings cannot be modified

Instance 1

import java.util.Arrays;

public class Test {
    public static void func(String s,char[] array){
        s = "author";
        array[0] = 'p';
    }
    public static void main(String[] args) {
        String str = "abcd";
        char[] chars = {'y','o','u'};
        func(str,chars);
        System.out.println(str);
        System.out.println(Arrays.toString(chars));
    }
}

Appendix

Conclusion:

Not that referencing can change the value of an argument.
You see, what exactly did this quote do!

Instance 2

public class Test {
    public static void main(String[] args) {
        String str1 = "hello";
        String str2 = new String("hello");
        System.out.println(str1 == str2);
    }
}

Design sketch

Appendix



Example 3

import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        String str1 = "hello";
        String str2 = "hello";
        System.out.println(str1==str2);
    }
}

Design sketch

Appendix

Instance 4

public class Test {
    public static void main(String[] args) {
        String str1 = "hello";
        String str2 = "he"+"llo";// Note: Both strings are constants at this point and are already "hello" when compiled
        // Simply put, if you stitch two string constants like this, they will be stitched by default at compile time, or a complete string constant by default
        System.out.println(str1 == str2);
    }
}

Design sketch

Appendix




Find main section

Instance 5

public class Test {
    public static void main(String[] args) {
        String str1 = "hello";
        String str2 = "he";
        String str3 = str1+"llo";
        System.out.println(str1 == str3);
    }
}

Design sketch

Instance 6

public class Test {
    public static void main(String[] args) {
        String str1 = "11";
        String str2 = new String("1")+ new String("1");
        System.out.println(str1==str2);
    }
}

Effect Graphics (the obvious answer is false, one complete, one stitch. They are definitely different)

Appendix (Bottom Level Analysis)


Example 7


Example 7 (Special Edition)

If I add a code str2.intern() after STR2 in the program; And?
What intern() does: manually pool its caller.

public class Test {
    public static void main(String[] args) {
        String str2 = new String("1")+ new String("1");
        str2.intern();
        String str1 = "11";
        System.out.println(str1==str2);
    }
}

Relapse of effect

Appendix

string comparison


If you are comparing variables of reference type and using the double sign to compare, you are comparing the addresses stored by the reference variable. I believe you should understand that the above examples are just like this.

equals

Syntax caller.equals();
If the call is data of reference type, it is important to note that the caller cannot have a null reference/pointer. Prevent null pointer exception errors



Expand

In Java, when data of type new String is used, there is no memory overlap. In Java, a string is an object, while in our new String object, a new object is generated. If the objects are the same, it is also called a new object?

Data of type String is immutable

Overall assignment of arrays, in Java, has only one chance, when defining arrays.

Example 1

Example 2

Example 3

Special References

StringBuilder

;public class Test2 {
    public static void main(String[] args) {
        String str = "hello";
        str =str + " world";
        str+="!!!";
        System.out.println(str);
    }
}

Appendix

Special: String-type data that can be modified by reflection

Or the last program, if I had to change a of the string "abcde" to g?
Yes, as we saw earlier, data of type String is stored on top as an array, and since it is an array, we can modify it by subscribing.
The problem, however, is that value's privates are private

So even if we get the object, we can't get the value's

But reflection goes there, and it's incredibly powerful.
What is reflection?
Take a visual example:
Every time we take the subway or the suitcases we carry, we need to go through security checks. As we know, the security checking machines emit a curve of medium spectrum. By reflecting, we can know what's in our suitcases. This reflects the concept of "reflection"

Here's an analogy, through Reflection. We can see that some of the properties stored in the class are either private or locked. I can see it,
That is, we can get all the information through reflection.

In fact, reflection is the biggest bug. If you use it well, it's called nice. If you don't use it well, it's called uncomfortable.

Keywords: Java Back-end

Added by turek on Mon, 22 Nov 2021 20:05:26 +0200