When Java Java creates a string, what happens in the string Pool?

Source: Internet
Author: User

The creation of a string for Java to be put into a string pool for code testing.

Excellent articles for reference

Java face question Series (ii)--How many string objects have been created?

public string (string original) API

Public String intern () API

Release Notes

JDK 1.7.0_71

New String () and intern ()
 Packagethe pool of com.nicchagil.commonstudy.No01String; Public classCall { Public Static voidMain (string[] args) {String a= "ABC"; String b= "ABC"; String C=NewString (a); String D= "AB" + "C"; String e= "ABC". Intern (); String F=NewString (a). Intern (); System.out.println ("A = = B:" + (A = =b)); System.out.println ("A = = C:" + (A = =c)); System.out.println ("A = = d:" + (A = =d)); System.out.println ("A = = e:" + (A = =e)); System.out.println ("A = = f:" + (A = =f)); }}

Log

Truefalse true True

Variable, constant string concatenation, do they put in string Pool?
 Packagethe pool of com.nicchagil.commonstudy.No01String; Public classCall2 { Public Static voidMain (string[] args) {FinalString A = "123"; FinalString B = "456"; String C= "123456"; String a= "123"; String b= "456"; System.out.println ("c = = (A + B):" + (c = = (A + b)));//A, B is final typeSystem.out.println ("c = = (\" 123\ "+ \" 456\ "):" + (c = = ("123" + "456"));//"123", "456" is constantSystem.out.println ("c = = (A + B):" + (c = = (A + b)));//A, B is a variableSystem.out.println ("c = = (A + \" 456\ "):" + (c = = (A + "456")));//A is a variableSystem.out.println ("c = = (A + b). Intern ():" + (c = = (A + b). Intern ()));//using the Intern () method    }}

Log

Truetrue falsefalse true

Attention

The results of this log print are the results of the JDK run above.

When Java Java creates a string, what happens in the 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.