A little understanding of string in Java

Source: Internet
Author: User

?? The string class is one of the most basic classes in Java, and the very best way to master it is very necessary!

? 1, the final type of string. is non-inheritable. The final class default method is the final type, which guarantees that the method cannot be overloaded. Member variables do not affect
? 2, the essence of string is char[] and its value is immutable
? 3, String str = "ABC" is a special way to create, "ABC" has a constant area, STR is only a reference to it
? 4. String can be concatenated by +. To form a new object
? 5. Java will maintain a string Pool when it executes. The string used to store the execution of the program, and the string inside is not repeated
? 6, the way to create a string is very much, summed up there are three categories:
???? One Use Newkeyword to create a string. For example, string s1 = new String ("abc");
??? ? second, direct designation.

For example, string s2 = "abc";
???? Third, use concatenation to generate a new string. For example, string s3 = "AB" + "C";?
?

public class MyString {public static void main (string[] args) {//About string stack parsing string str1 = "abc"; SYSTEM.OUT.PRINTLN ("abc" = = str1),/*1, allocating memory on the stack str1 2, the string pool to open up a space.  Holds the string constant "ABC"; 3, str1 points to "ABC" in the string pool; 4. Output true */string str2 = new String ("abc"); System.out.println (str1 = = str2);/*1, allocating memory on the stack str2 2, at this time the "ABC" already exists, and then the "ABC" reference to the constructor of string. Create a new string object on the heap then the JVM still arranges for "ABC" to IS in this pool, and stillprovides a reference to it, but tha  T reference is then passed into Aconstructor to create a NEW String object this ' s not in the pool andcopies the first one.  It's This second String object which ends upbeing assigned to the reference 3, str2 points to the newly created object on the heap. 4, Output false */string STR3 = "a" + "BC"; SYSTEM.OUT.PRINTLN ("abc" = = Str3),/*1, stack allocated memory storage STR3 2, based on the compiler to merge known amount of optimization function, the pool to open a space, the merged string constant "ABC" 3, Str3 point to the heap "ABC "' 4, Output true */string STR4 =" a "; String str5 = "BC"; String STR6 =STR4+STR5; SYSTEM.OUT.PRINTLN ("abc" = = STR6);/*1) stack in the middle of the storage reference s1,s1 point to the pool string constant "a". 2) Open a piece of the stackReference s2 are stored between the S2 points to the pool in string constant "B", 3) stack to open up a middle storage reference STR5, 4) S1 + S2 through the last step of StringBuilder the ToString () method restores a new string object "AB", so a space in the heap to hold this object , 5) References STR6 to the new string object that is restored in the heap (S1 + s2), 6) STR6 points to the object in the heap, and the constant "AB" is in the pool, the output is false */string STR7 = new String ("abc"); String str8 = new String ("abc"), if (STR7 = = str8) {System.out.println ("Str7 equals Str8");} Else{system.out.println ("Str7 not equal to Str8");} Output STR7 not equal to Str8}}


?

A little understanding of string in Java

Related Article

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.