Java zero foundation entry series – Day6 Java string

String is our most commonly used type. Each string represented by double quotation marks is a string. A string in Java is a predefined class. Like C + +, it is called string instead of char array. As for what is called a class, we will not introduce it too much for the time being. There will be a detailed introduction about the class in the following chapters. Here, we only need to understand the class as a mold, just like making moon cakes. If we want moon cakes with any appearance, we can select the corresponding mold, press it, and then put it in the oven to get the moon cakes we want. The moon cakes made with the same mold are basically the same. Of course, this metaphor is not very appropriate, but for now, it is effective. After declaring a variable of string type, the variable is an instance of string type, just like a moon cake made by a mold. It can use all methods of string type and has all properties of string.

String class has some basic methods, such as substring, splicing, equality detection and other commonly used APIs. String processing will be frequently used, so skilled use of string will be indispensable. Not much to say, let's go directly to the code practice.

I won't say more about the method of creating a new project. The code is as follows:

package pers.frank.test;
public class Test {
static void main(String[] args){
String str = "我爱Java!!!";
String str1 = str.substring(0,4);
System.out.println("str1:"+str1);
String str2 = str + "但我也爱C++。"
System.out.println("str2:"+str2);
String str3 = "我爱Java!!!"
System.out.println(str3.equals(str));
System.out.println(str == str3);
System.out.println(str.length());
System.out.println(str.charAt(0));
System.out.println(str.charAt(2
System.out.println(str.charAt(4
String str4 = "我爱JavaScript!!!"
System.out.println(str4.substring(0,4) == str1);
}
}

Line 5 defines a string variable STR and initializes it. The sixth line calls the substring method of string and takes the first four characters. This method has two parameters. The first parameter represents the position of the first character you want to get, starting from 0, and the second character represents the position of the first character you don't want to get. Therefore, str.substring (0,4) takes the first four characters of the string str.

Line 9 defines STR2, which is initialized with the string "but I also love c + +." It is spliced with "+" to form a new string.

Line 12 defines str3, which is also initialized and assigned as "I love java!!!", The equals method of string is used to determine whether the contents of two strings are the same, because str3.0 is called When equals (STR), it will return true. Note that "= =" is not used to judge whether the two strings are the same. Although it can be executed smoothly by using the equal sign in some cases, it is not the case in all cases. When the "= =" sign is used to judge whether two string variables point to the same string constant at the same time, rather than comparing their contents. String is more like char * here. String variables do not really store the contents of strings, but their location in memory, just like your home address is written in the household registration book, rather than moving your home to the registration book. When judging with the equal sign, the comparison is whether the contents recorded in the register are the same, The equals method compares whether the two addresses point to the same house (it may not be the same house). In Java, string constants are stored separately in memory space. When declaring STR and STR2, two string constants will be created and stored in memory space, and then their addresses in memory space will be assigned to these two variables. When declaring str3, no new string constant will be created, because the java compiler will let String constants are shared, so str3 and STR actually point to the same address. At this time, when the equal sign is used to judge whether the two are equal, the correct result can be obtained. But it doesn't apply to all cases. If you declare another string variable str4 and assign the value "I love JavaScript!!!", When the substring method is called again and compared with STR1 with an equal sign, the correct result cannot be obtained.

Line 15 calls the length method, which returns the length of the string.

Lines 16-18 call the charat method and return the character at the first position. The following are the running results:

Because all strings in Java are Unicode characters, the concept of characters here is different from that in a language. Now that I've said this, I'll explain the relevant concepts in detail.

A character is the smallest unit of abstract text. It has no fixed shape (possibly a glyph) and no value. "A" is a character and "€" (the symbol of the common currency of Germany, France and many other European countries) is also a character.

A character set is a collection of characters. For example, Chinese characters are the first characters invented by the Chinese people and are used in Chinese, Japanese, Korean and Vietnamese writing.

An encoded character set is a character set that assigns a unique number to each character. The core of the Unicode standard is a coded character set. The encoding of the letter "a" is 004116 and the encoding of the character "€" is 20ac16 The Unicode standard always uses hexadecimal digits and prefixes "U +" when writing, so the encoding of "a" is written as "U + 0041".

A code point is a number that can be used to encode a character set. The coded character set defines a valid range of code points, but does not necessarily assign characters to all these code points. Valid Unicode code point ranges are u + 0000 to U + 10ffff Unicode 4.0 assigns characters to 96382 of more than one million code points.

In more detail, we may have to talk about the specific coding. Since it is not the focus of this article, we still don't introduce it too much. There will be an article devoted to it later.

There are many methods about string, but there are not many commonly used methods. I won't introduce them all here. If you want to know, you can check here: http://www.runoob.com/java/java-string.html

For the time being, you don't need to remember all of them. As long as you know that there is such a function, you can realize such a function. The future multi code code code can be remembered naturally.

Finally, we introduce a class called StringBuilder. What is this class for? It is mainly used for splicing and processing of multiple strings. Then you may ask, since "+" can splice two strings, why do you need this class? In fact, every time a new string variable is declared and given an initial value, if the string constant does not exist, a new string constant will be created and stored. Just like when STR2 is declared above, a new string object "I love java!!! But I also love c + +" will be created Instead of simply "but I also love c + +." It is added after STR, and if it is spliced many times, especially when used in the loop, this method will undoubtedly waste a lot of space. What we need is the last string, not the string generated in the middle of the process. Therefore, the string object generated in the process does not need to be stored, which is a waste of storage for the program, At this time, StringBuilder is generated. You can call its append method to splice strings, which can save a lot of unnecessary waste of space.

In fact, if you know C or C + +, you should be familiar with char []. StringBuilder has a char array maintained by itself, which can be spliced and processed by dynamically applying for memory.

The common methods of StringBuilder are as follows:

Similarly, you don't need to memorize all by rote. You can master it by using it more in later exercises.

So far, the explanation of string has come to an end. Welcome to continue your attention!

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
分享
二维码
< <上一篇
下一篇>>