Java String Class

Source: Internet
Author: User

java.lang.String; is a string type, the first thing to note about the string class is the following:

1. Once the string is created, it cannot be changed. Once the "ABC" string object is created, it can no longer become "ABCD";

2. Improve the access efficiency of strings: "Caching" technology is used in the program. So in Java all use "double quotes"

Strings are created in the string constant pool. The string constant pool is stored in the method area.

3. During program execution, if a program uses a string, such as "ABC", the program will be in the string constant pool

Create a new "ABC" string and use it directly if found. (The string constant pool is a buffer, in order to improve the efficiency of the access string)

PublicClassstringtest01{
PublicStaticvoidMainString[] (args) {
Create an "ABC" string object, the memory address of the object, and let the S1 variable be saved.
S1 is a reference, and S1 points to the "ABC" object.
Strings1="ABC";
You can make S1 point back to
But the "def" string itself is immutable
s1="Def";
StringS2="Hello";Creates a new "Hello" string object in a string constant pool that is not mutable
Strings3="Hello";directly from the string constant pool.
System.out.println (S2==S3);True
//Determine if string equality cannot be used = =
string s4=new string ("abc");
string s5=new string ("abc");
System.out.println (S4==S5); //false
//The following program will create three string objects "AAA" "BBB" "AAABBB" in the string constant pool after execution ends
String s6="AAA";
String s7="BBB";
String s8=s6+s7;
}
}

In the above code, first create an "ABC" string object, and the memory address of the object, let the S1 variable is saved, that is, S1 is a reference, pointing to the "ABC" object. S1= "Def"; is to re-point the S1 to the string "Def", but the "def" string itself is immutable. A new string s2,string s2= "Hello" is created, the object is immutable in the string constant pool, and the string is found in the string constant pool when the "Hello" string is created, and no re-creation is required. , you can use it directly, that is, the third one that you just started with, which can improve the efficiency of accessing strings. When using string, we should also note: Try not to do string concatenation operations, such as the above strings S8=S6+S7, because once the string created immutable, as long as the frequent splicing, will be in the string constant pool to create a large number of string objects, garbage collection caused problems.

Next look at different ways to create the difference between string objects.

1.String s1= "ABC"; only One "ABC" string object is created in the string constant pool;

2.String s2=new string ("Hello"), a "Hello" string object is created in the string constant pool, and a string object is created in the heap.

The second way to compare wasted memory, commonly used is the first way. OK, let's do a question, how many objects are created by using the knowledge you just told?

public class StringTest03{
 public static void main(String[] args){
   String s1=new String("hello");
   String s2=new String("hello");
   
 }
}

The answer is three, do not know if you have done right?

String S1=new string ("Hello");

String S2=new string ("Hello");

New has two string objects, so there are two objects in the heap, because all strings in Java that use "double quotes" are created in the string constant pool, and the string constant pool is stored in the method area, so the string constant pool in the method area is one of the two constant pools in the heap, is the final answer.

Finally, take a look at the common construction methods for strings.

/*
About string Common construction methods
*/
PublicClassstringtest05{
PublicStaticvoidMainString[] (args) {
1.
Strings1="ABC";
2.
StringS2=NewString ("ABC");
3.
Byte[] bytes={97,98,99,100};
Strings3=NewString (bytes);
System.out.println (S3);ABCD string has overridden the ToString method in object
4.
Strings4=NewString (Bytes, 1, 2);    //1 for start subscript, 2 for length
    System.out.println (S4);    //bc
    //5.
   char[] c1={ ' i ', ' is ', ' in ', ' state ', ' person '};
    string s5= New   String (C1);
   SYSTEM.OUT.PRINTLN (S5);    //I am Chinese
    //6.
    string s6= New   String (C1, 2, 2);
   system.out.println (S6);   //China
   }
}

Java String class

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.