Java 0 Basic Starter series Day6 Java string

Source: Internet
Author: User

The string, which is our most common type, is a string for each string represented by double quotation marks. The string in Java is a predefined class, like C + +, which is called a string, not a char array. As to what is called class, do not introduce too much, in the following chapter will have a detailed introduction to the class, here, just want to understand the class as a mold, just like the moon cakes, want to what the appearance of the moon cakes, choose the corresponding mold a pressure, and then go to the oven to put, you can 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 the time being, it is effective. After declaring a variable of type string, this variable is an instance of the string type, like a moon cake made by a stencil, and can use all methods of the string type, as well as all the properties of string.

String This class has some basic methods, such as fetching substrings, stitching, detecting equality and other common APIs. String processing is often used, so the skillful use of string will be indispensable. Not much to say, we are directly on the code to exercise.

The method of new project, I will not say more. The code is as follows:

1  Packagepers.frank.test;2 3  Public classTest {4      Public Static voidMain (string[] args) {5String str = "I love java!!! ";6String str1 = str.substring (0,4);7System.out.println ("str1:" +str1);8 9String str2 = str + "But I also love C + +. ";TenSystem.out.println ("str2:" +str2); One  AString STR3 = "I love java!!! "; - System.out.println (Str3.equals (str)); -System.out.println (str = =STR3); the 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 = "I love javascript!!! "; +System.out.println (str4.substring (0,4) = =str1); A     } at}

The 5th line defines a string variable, str, and assigns it the value of the initialization. Line six, called the substring method of string, takes the first four characters, this method altogether two parameters, the first parameter represents the position of the first character to be taken, starting from 0, the second character represents the position of the first character that you do not want to take. So Str.substring (0,4) takes the first four characters of the string str.

Line 9th defines the STR2, when initializing the str with the string "But I also love C + +. The "+" number is stitched together to form a new string.

Line 12th defines the STR3, and the same initialization is assigned to "I love java!!! ", the Equals method of string is used to determine whether the contents of two strings are the same, because when calling Str3.equals (str), it returns true, noting that two strings are not used" = = "at the same time, although in some cases, the use of the equals sign can be executed smoothly, But not all of this is true. When judging with the "= =" number, it is judged whether the two string variables point to the same string constant instead of comparing their contents. The string here is more like char*, where the strings do not actually store the contents of the string, but rather store their location in memory, just like the address of your home is written in the hukou, instead of moving your home into the register. When judging by the equal sign, the comparison is whether the contents of the register are the same, and the Equals method compares the two addresses to the same house (which may not be the same house). In Java, string constants are stored separately in memory space, when declaring Str and STR2, a new two string constants are created and stored in the memory space, and then they are assigned to the two variables in the memory space address. When declaring STR3, there is no new string constant, because the Java compiler will let the string constants be shared, so the STR3 is actually pointing to the same address as Str. The correct result can be obtained by using the equal sign to determine whether the two are equal. But it does not apply to all cases, if you declare a string variable STR4, assign the value "I love javascript!!! , and then call the substring method to compare with the str1 with the equals sign, you will not get the correct result.

Line 15th calls the length method, which returns the lengths of the strings.

The 第16-18 line invokes the Charat method, returning the characters at the first position. The following is the result of the operation:

  

Because the strings in Java are Unicode characters, the concept of character here is not the same as the concept of a character in a language, and since it is here, it will give you a detailed explanation of the relevant concepts.

The character is the smallest literal unit of abstraction. It has no fixed shape (possibly a glyph) and has no value. "A" is a character, "€" (a symbol of the general 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, and are used in Chinese, Japanese, Korean, and Vietnamese writings.

The coded 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 character "€" is encoded as the 20ac16.unicode standard always uses a hexadecimal number, and when written with the prefix "u+", so "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 code point range, but does not necessarily assign characters to all of these code points. Valid Unicode code point ranges are u+0000 through U+10FFFF. Unicode 4.0 assigns characters to the 96382 code point in the distances code point.

Further details may be to mention the specific code up, because it is not the focus of this article, so still do not do too much introduction, then there will be articles specifically introduced.

There are a lot of methods about string, but not many, not all of them here, want to know the words can be viewed here: http://www.runoob.com/java/java-string.html

Temporarily do not need to remember all, as long as you know there is a function, you can achieve that kind of function, the future of multi-code code can naturally remember.

Finally, we introduce a class called StringBuilder. What does this class do? Mainly used for multi-string splicing and processing. Then you may ask, since the "+" will be two strings together, why do you want this class? In fact, each time a new string variable is declared and given an initial value, if the string constant does not exist, a new string constant is created and stored, as if STR2 was declared above, creating a new String Object "I love java!!!. But I also love C + +. "Instead of simply going" but I also love C + +. "After Str, and if there are multiple splicing, especially in the loop to use, this way will undoubtedly waste a lot of space, we need only the last string, rather than the middle of the process of the resulting string, so the process generated by the string object is not necessary to store, to the program is a waste of storage , this time StringBuilder is generated, you can call its Append method to stitch strings, so you can save a lot of unnecessary waste of space.

In fact, if you have known C or C + +, char[] should be more familiar with, StringBuilder inside is a self-maintained char array, through the dynamic application of memory mode for splicing and processing.

StringBuilder common methods are as follows:

The same does not require all rote learning, in the subsequent exercises to use more skillfully mastered.

At this point, the explanation of the string is over, please continue to pay attention!

Java 0 Basic Starter series Day6 Java string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.