Creation of Java string A=new string ("ABC")

Source: Internet
Author: User

Topic

string s = new string ("Hello") and string s = "Hello";

Difference

string s = new string ("Hello") creates a 2 (1) object, and string s = "Hello" creates 1 (0) objects.
Note: When the string constant is in the pool there is an object hello in parentheses!

Introduced

The difference between = = and Equals ():

    1. = =: Compare the reference type comparison is the address value is the same
    2. equals: Comparing reference types By default is also the same as comparing address values, and the string class overrides the Equals () method to compare whether the content is the same.
Demo
public Span class= "Hljs-keyword" >class StringDemo2 {public static Span class= "Hljs-keyword" >void main (string[] args) {String S1 =  New String ( "hello"); String s2 =  "hello"; System. out.println (S1 = = s2); //false System. out.println (s1.equals (S2)); //True}} * * Run results:**> false > true     
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
Memory diagram

Code explanation
    1. First, the stack is entered through the main () method.
    2. Then in the stack to define an object S1, go to the heap to open up a memory space, the memory space reference to S1, "hello" is a constant, and then go to the string constant pool to see if there is a Hello string object, no words allocated a space to store Hello, and put its space address into the new space in the heap.
    3. Define an object S2 in the stack, and then go to the string constant pool to see if there is a "hello" string object, with the address of "hello" directly assigned to S2.
    4. That is, the space allocated in the heap is stored in the S1, and the space allocated in the heap is the address value of the space in the string constant pool where the allocated space holds "Hello". In S2, there is the address value of the space in the string constant pool where the allocated space holds "Hello".
    5. The output is false because the S1 is different from the address stored in the S2. Because the class string overrides the Equals () method, it compares the value of the reference type to equality, so the output is true. That is, the result is false, true.
Demo1
PublicClass StringDemo1 {PublicStaticvoidMain (string[] args) {String S1 =New String ("Hello"); String s2 =new String ( "hello"); System. out.println (S1 = = s2); //false System. out.println (s1.equals (S2)); //true string s3 = new String ( " Hello "); String S4 =  "hello"; System. out.println (s3 = = S4); //false System. out.println (S3.equals (S4)); //true String s5 =  "hello"; String s6 =  "hello"; System. out.println (s5 = = S6); //true System. out.println (S5.equals (S6)); //True}}             
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

Demo1 detailed

S1~S6 is not explained by comparison with Equals (), both are values that are compared and are true. The following explanation = =

    1. S1, S2: Both are new, each in the heap allocated space, and each of the memory address assigned to S1, S2. The space address is different, = = is compared to false. However, each of the values stored in the heap space is the address of the same object in the string constant pool. It is not difficult to understand according to the diagram at the demo.
    2. S3, S4 Ibid demo explanation.
    3. Both S5 and S6 are values in the constant pool, both pointing to the same object in the constant pool with the same address value, so the result is true.
Demo2
PublicClass StringDemo4 {public static void main (string[] args) {String S1 = " hello "; String s2 =  "world"; String s3 =  "HelloWorld"; System. out.println (s3 = s1 + s2); //false System. out.println (s3.equals (s1 + s2)); //true System. out.println (s3 =  "hello" +  "world" ); //false System. out.println (s3.equals ( "hello" +  " World ")); //True}}             
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

Demo2 detailed

The Equals () comparison method does not explain that the comparison values are equal and are true.

    1. S1 and S2 Add is first in the string constant pool open a space, and then splicing, the address of this space is S1 and S2 after stitching the address. The output is false because it differs from the address of the S3.
    2. S3 and "Hello" + "world" For comparison, "Hello" + "world" first stitching into "HelloWorld", and then go to the string constant pool to find whether there is "HelloWorld", there is, so and s3 a string object is shared, true.
Summarize
      1. string s = new string ("Hello") creates a 2 (1) object, and string s = "Hello" creates 1 (0) objects.
        Note: When the string constant is in the pool there is an object hello in parentheses!
      2. String if the variable is added, first open space, in stitching.
      3. If the string is a constant addition, it is added first, then the constant pool is searched, if there is a direct return, otherwise, it is created.

Creation of Java string A=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.