Differences between two ways to create a string in Java

Source: Internet
Author: User


We know that there are usually two ways to create a string in Java, which is directly assigned by double quotation marks and created by the constructor.


String x = "ABCD"; String y = new String ("ABCD");


However, what is the difference between these two ways? Which cases are applied separately, not quite understood before.


1, double quotation marks the way

String x = "ABCD"; String y = "ABCD"; System.out.println (x==y);//truesystem.out.println (X.equals (y));//true


The above output results:

The result of X==y is true because after the way the double quotation marks are assigned, x and Y both point to the same memory address, their references are to the same content in the method area, the reference address is the same, and when the same string literal is created no matter how many times, there is always only one memory address assigned. This is followed by a copy of the string, called "string Dwell" in Java, where all string constants will automatically reside after compilation. x = "ABCD" This method is created when first looking at whether the string pool is already present, directly returning the string object in PermGen, otherwise a new string object is created and then put into the string pool.


In the JVM, considering the convenience of garbage collection (garbage Collection), the heap is divided into three parts: young Generation (Cenozoic), tenured generation (old generation), and Permanent Generation (PermGen) (permanent generation)


String pooling is to solve the problem of string duplication, long life cycle, which exists in PermGen.


When compiling Java source code, the strings in the double quotes that appear in the source file are stored in a constant pool, with constant_utf8_info entries.


In the JVM, when the corresponding class is loaded and run, the constant pool maps to the JVM's running constant pool. Each of these constant_utf8_info (which also tries to record those strings) will automatically generate the corresponding internal string, recorded in the string pool, when the constant reference is resolved.


2, the way of the constructor

String a = new string ("ABCD"); String b = new String ("ABCD"); System.out.println (a==b);//falsesystem.out.println (A.equals (b));//true


The above code runs the result:

A==b is false because two memory addresses were allocated in the heap heap after the method was created by the new constructor. A and B point to two different objects in the heap, and different objects have different address assignments.


The following diagram is a good illustration of the above two things:




A string-resident operation will still exist at run time, even if it was created through a constructor.


String c = new String ("ABCD"). Intern (); String d = new String ("ABCD"). Intern (); System.out.println (c = = d);  Now TrueSystem.out.println (C.equals (d)); True

The application situation:


It is recommended that you use string x = "ABCD" in your usual application to create strings instead of string x = new String ("ABCD"), because the new constructor will certainly open up a heap of heap space, The double-quote method is optimized using string interning (String dwell), which is more efficient than the constructor.


If for some reason you do need to create a new string object in the heap, you can use the constructor to resolve it.


Of course, these are just my own understanding, because it is also a novice, the time to contact Java is not long, there must be a lot of things to understand the wrong, the question of the string resides in the interpretation is not very thorough, just know some simple general meaning, I hope that Daniel criticized, thank!


Original address:

http://www.programcreek.com/2014/03/create-java-string-by-double-quotes-vs-by-constructor/

Differences between two ways to create a string in Java

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.