String str= "abc" with string str = new String ("abc")

Source: Internet
Author: User

The 1.java runtime environment has a string pool, maintained by the string class, that executes the statement string str= "abc":

1. First see if there is a string "abc" in the string pool, assign "ABC" directly to STR if it exists, and if it does not exist, create a new string "abc" in the string pool before assigning it to Str.

2. Execute the statement string str = new String ("abc"); Creates a new string "ABC", regardless of whether the string "ABC" exists in the string pool, (note that the new string "ABC" is not in the string pool) and assigns it to STR

This shows that 1. Efficiency is higher than 2 efficiency.

3. String str1= "Java";//point to String Pool

String str2= "blog";//point to String Pool

String s = str1+str2; The + operator establishes two string objects in the heap, the values of which are "Java", "blog", that is, the two values are copied from the string constant pool, and then two objects are created in the heap.  The object s is then created, and the heap address of "Javablog" is assigned to S. This sentence creates a total of 3 string objects.

System.out.println (s== "Javablog");//The result is false;

The JVM does have an object in the form of a string str= "Javablog", which is placed in a constant pool, but it is at compile-time name. The string s=str1+str2 is known at run time, meaning that STR1+STR2 is created in the heap, so the result is false.

String S= "java" + "blog";//The Javablog object is placed directly into the string pool. System.out.println (s== "Javablog");//The result is true;

String s=str1+ "blog";//Not placed in the string pool, but distributed in the heap. System.out.println (s== "Javablog");//The result is false;

In summary, there are two ways to create a string: Two memory regions (POOL,HEAP)

1. "" Creates a string in the string pool.

2.new when creating a string, first look at whether there is the same string in the pool, if any, copy one copy into the heap, and then return the address in the heap, if none in the pool creates a point in the heap, and then returns the address in the heap.

3. When assigning a value to a string, if the right operand contains one or more string references, then a string object is established in the heap, and a reference such as String s= str1+ "blog" is returned;

String str= "abc" with string str = new String ("abc")

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.