Java String new = with starts

Source: Internet
Author: User


1. First, String does not belong to eight basic data types. String is an object.

Because the default value of an object is null, the default value of String is also null, but it is a special object and has some features that other objects do not have.

2. Both new String () and new String ("") declare a new null String, which is an empty String or not null;

3. String str = "kvill ";
String str = new String ("kvill"); difference:

Here, we will not talk about heap or stack, but simply introduce the simple concept of constant pool.

The constant pool refers to the data that is identified during the compilation period and saved in the compiled. class file. It includes constants in classes, methods, interfaces, and other fields, as well as string constants.

Example 1:


String s0 = "kvill ";


String s1 = "kvill ";


String s2 = "kv" + "ill ";


System. out. println (s0 = s1 );


System. out. println (s0 = s2 );


Result:

 


True


True


First, we need to know that Java will ensure that a String constant has only one copy.

 

Because "kvill" in s0 and s1 in the example are string constants, they are determined during the compilation period, so s0 = s1 is true; both "kv" and "ill" are characters.
String constant. When a string is connected by multiple string constants, it must also be a String constant. So s2 is also parsed as a String constant during compilation, so s2 is also a constant pool.
A reference of "kvill.

 

So we get s0 = s1 = s2;

 

The String created with new String () is not a constant and cannot be determined during the compilation period. Therefore, the strings created with new String () are not placed in the constant pool and they have their own address space.

 

Example 2:

 


String s0 = "kvill ";


String s1 = new String ("kvill ");


String s2 = "kv" + new String ("ill ");


System. out. println (s0 = s1 );


System. out. println (s0 = s2 );


System. out. println (s1 = s2 );


Result:

 


False


False


False


In Example 2, s0 is still a "kvill" application in the constant pool. s1 is a reference of the new object "kvill" created at runtime because it cannot be determined during the compilation period, s2, because there is a second half, new
The String ("ill") cannot be determined during the compilation period, so it is also an application that creates the "kvill" object. If you understand this, you will know why this result is obtained.

 
Author: "henry-cong"
 

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.