A little understanding of the string class in Java

Source: Internet
Author: User

This is the basic knowledge used in learning

A String is a class that is often used to learn the summary of what is found during the process.

1. Initialization aspects:

The string class is a special class that is initialized with a special syntax that is simplified.

1>string s = "abc";

And according to the normal grammar, it should be:

2>string s = new String ("abc");

The recommended way to use 1> is that this saves memory space, and it is said from other websites that "there is a lot of waste in memory usage just in terms of object-oriented standard syntax." For example, string s = new String ("abc"), actually creates two string objects, one is an "ABC" object, is stored in a constant space, and one is the space requested for the object s using the New keyword. 】

Here's the simple code

  

1         String a = "abc"; 2         String B = "abc"; 3         System.out.println (A==B);

The result of printing is true. The code here means that a reference variable of type string is created, and A and b,a and B point to the string class with the value "ABC", as to whether the object is a new string type. It needs to be analyzed according to the contextual of the code (because the JVM will automatically decide if it is necessary to create a new object based on the actual data in the stack). The result is true, which means that at least the second line of code does not recreate a string type object, but instead points to the existing "ABC" object, so a and B point to the same object, which is bound to be equal.

Again, as in the following code:

1         New String ("abc"); 2         New String ("abc"); 3         System.out.println (d==e);

The result of printing is false, D, e two references to different objects, 1, and 2 lines of code are the processing logic: whether or not their string values are equal, a new object will be created if a new object is created.

In addition to the study here, found in code 1, proposed a design pattern of the idea--------------------the object of reusing existing objects in memory, reducing the performance consumption of the system creation object instance. As for more analysis, there may be a gradual deepening of learning

2. Read-only characteristics of string

A string is a constant that, when initialized, cannot be changed once created, so it appears to be a method of modifying a string value, not the logic inside. As in the following code:

  

1 String a1 = "First"; 2 System.out.println (A1 + ", Second");

After printing is "first, second", in fact, in the internal logic, actually generated a new string object, rather than the original string object appended to the content.

The internal processing of Java is said to be through the StringBuilder class to achieve the concatenation of strings. So the bold assumption is that the above code is equivalent to the following implementation:

1 New StringBuilder (); 2 Stringbuilder.append (A1); 3 stringbuilder.append (", second"); 4 System.out.println (stringbuilder.tostring ());

As can be seen in the above code, if the hypothesis is the case, when the string concatenation when the use of the "string +" way to do, and in Java is through the StringBuilder way to achieve, That would create a StringBuilder object to handle, and there wouldn't be a lot of extra consumption in a single case, but if you use a "String +" in the loop body, you'll continue to go into the loop and misbehave create the StringBuilder object. This is a suspicion of increasing memory costs. So if all of the above assumptions are true, is it better to use StringBuilder when using string concatenation? The StringBuilder object is constantly created in each loop after the StringBuilder is converted into a single mode.

3. The difference between equals and "= ="

Simply put, the Equals () method of String compares the characters in a string object, and the = = operator Compares whether two objects refer to the same instance, and the code is as follows:

1         String S1 = "Hello"; 2         New String (S1); 3         //  4         //False

This code is relatively simple, see below is the implementation method of equals

 Public Booleanequals (Object anobject) {if( This= = AnObject) {//determine if the same object        return true; }    if(AnObjectinstanceofString) {String anotherstring= (String) anobject;//String Type        intn =count; //A character comparison of one character        if(n = =anotherstring.count) {CharV1[] =value; CharV2[] =Anotherstring.value; inti =offset; intj =Anotherstring.offset;  while(n--! = 0) {            if(v1[i++]! = v2[j++])            return false; }        return true; }    }    return false; }    

It can be seen from the source code that the comparison is whether the characters in the string are exactly equal. Look at the source code is a good habit, to have patience.

  

A little understanding of the string class in Java

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.