Java String Pool

Source: Internet
Author: User


The so-called World article is copied, find a Java string pool of articles on the Internet, search n more than one entry, it seems to look at two articles, summarized as follows:

First look at a piece of code:

String a = "Hello";

String B = "Hello";

String c = new String ("Hello");

String d = new String ("Hello");

System.out.println (A = = B);

System.out.println (b = = c);

System.out.println (c = = d);

System.out.println (A.equals (b));

System.out.println (B.equals (c));

System.out.println (C.equals (d));

The result is true,false,false,true,true,true. The string pool is involved here. "String Pool" is the measure that Java uses to improve memory utilization: when encountering string a = "Hello"; When such a statement is made, Java will first look in the string pool for whether the string "Hello" already exists, if not, establish the string "Hello" object, then the variable a points to the address, and then encounter the statement string b = "Hello", the string pool already has " Hello ", so just let the variable B also point to this address, eliminating the hassle of redistribution. In Java, the operator "= =" for the two basic type, is to determine whether the content is the same, for two objects, it is to determine whether the address is the same, so a = = B returns True. So what about string c = new String ("Hello")? If this is the case, then the string pool will not be accessed, instead the variable C is first opened up and then the value is written to space. So a = = c returns false,c = = D also returns false. As for the Equals method of the string, because it compares the object's address to the object's value, it is not surprising that it returns true.

The Java Virtual machine has a string pool that holds almost all of the string objects. A string expression always points to an object in the string pool. A string object created with the new action does not point to an object in the string pool but you can use the Intern method to point to an object in the string pool (note: If the pool already has the same string--using the Equals method, the string in the pool is returned, or the string is first added to the pool. Back again). Two equal strings in the pool will return true if they are compared using "= =".


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.