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

Source: Internet
Author: User
Tags constant tostring

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;

The difference between object O and Object o=null:

The specific differences are as follows:
Class Test {

public static void Main (string[] args) {
Object O1;
O1.tostring (); /* Compiler cannot pass here, the compiler only determines that O1 is a reference and does not point to any object, so the method cannot be called. */
Object O2 = null;
O2.tostring (); /* This can be compiled, but with a null pointer exception, the compiler determines that O2 is an object, although it is an empty object. */
}
}
A null object is a special object that can be of any type. He's just a mark, just for the sake of a mark that doesn't exist. There is no need to pursue him in memory. Null is a token. A container can accept an empty object, but a null reference is not acceptable.
Object o; This notation only assigns a reference and does not allocate memory.
Object o = null; In this case, the reference is directed to an empty object. Allocated memory (empty object), so compile no error, run times null pointer exception.

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.