Java String Pool

Source: Internet
Author: User

1. Two constructors of a string
Private Final Char value[]; Private int Hash;  Public String () {    this. Value = "". Value;}
Public string (string original) { this. Value = original.value; this. hash = Original.hash;}
2. How many string objects are created in the new string? 1) Creation of two string objects
 Public Static void Main (string[] args) {    new String ("Hello");}

The memory model is as follows:

2) Create a String object
 Public Static void Main (string[] args) {    = "Hello";     New String ("Hello");}

The memory model is as follows:

3. Intern method of String

The string class privately maintains an initially empty string pool when the Intern method is called:

1. If the string pool already contains a string equal to this string object (as determined by the Equals method ), the string in the pool is returned.
2. Otherwise, add this string object to the pool and return a reference to this string object.

 Public native String intern ();

A String object reference is placed in the pool of strings, not the string object itself.

Experiment:

 Public Static void Main (string[] args) {    = "hel";     + = "Lo"    ; = "Ja";     + = "va"    ; // true    // false}

y = = Y.intern () False indicates that "Java" already exists in the string pool, and the string loaded in the string pool when the JVM starts also includes: "True", "false" ...

Memory model:

Intern ago

After intern

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.