java-String Pool

Source: Internet
Author: User

1. Reasons for use:

In peacetime development, the use of a large number of string types, if you use a string, you use new to generate, will consume memory. Sun Corporation uses the concept of string pooling to solve this problem.

2. Literal/Direct Volume

The so-called direct volume is at the time of definition, the right side of the equal sign is not another variable, but a meaningful amount of actual existence. such as

String s= "a";//Right is the direct amount of string s1=s;//to the right is not the direct amount
3. Rules:

In Java, if we define a string equal to the right is a direct amount, then the direct amount exists in the string pool, if in the definition of an identical direct amount, will first go to the string pool to look for, if there is a direct point to the pool object, if not exist to create.

String s= "a"; String s1= "a"; String S2=new string ("a");
There is the above code: first of all to determine the right assignment is the direct quantity then the object is stored in the string, the buyers to its address, the second statement is to face the amount, first in the string pool to find the existence of the string, and found, then the buyers store S1 address also points to the address in the string pool. And the third statement is not the same, because it is generated with new, then it is in the heap area to open up memory. Obviously S2 points to a different address than s,s1.

4, the String object has a very error-prone features:
If you invoke a method on a string object, whether the object is in the pool or in the heap, if you call the
method does not change the previous string object, then the calling method does not produce a new object, which is also the word
An optimization mechanism of character string

5.intern method

The method is to artificially put the string object placed in the heap area into the string pool has been optimized means.

Code: (Able to do all right even if mastered)

Package Com.string;public class Stringdemo {public static final string st= "AB";p ublic static final string st1= "a";p ublic s tatic void Main (string[] args) {String s0=st1+ "B"; String s= "a";//Right is the direct amount of string s1=s;//to the right is not a direct amount of string s2=s+ "B"; String s3= "12ABCD3"; String s4= "12ABCD3" + ""; String s5= "12ab" + "CD3"; String s6= "12AB" + "CD" + ' 3 '; String s7= ' 1 ' + ' 2 ' + "AB" + "CD" + ' 3 '; String s8= "B"; String S9=new string ("Abcd2"); String S10=new string ("abc" + "D2"); String str1 = "abc"; String str2 = Str1.concat (""); String STR3 = "abc". SUBSTRING (0); System.out.println (s0==st);//true, constant is also the literal, the direct amount of the operation at compile time System.out.println (S==S1); True, the value of S is assigned to S1SYSTEM.OUT.PRINTLN (ST==S2) during run time,//false, where S is the variable, the addition is run in the heap area, and an object is regenerated in the heap. System.out.println (S3==S4);//true, the System.out.println (S3==S5) at compile time for the direct volume operation;//true,system.out.println (S3==S6); /true, this is related to the operation of the string, from left to right, the left side is a string, the right side is loaded with a transposition string. System.out.println (S3==S7);//false, similar to the previous one, first two characters added remember that the ASCLL code value is added, in the reload as a string. System.out.println (st== (S+S8))//falsesystem.out.println (S9==S10)//false, two different objects, the address must notWith System.out.println (STR1==STR2);//true, see 4. Error System.out.println (STR1==STR3);//true//intern use string A= "Java"; String B=new string ("Java"); String C=b.intern (); System.out.println (a==b);//false, see 5system.out.println (A==C);//true}}


java-String Pool

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.