String type in Java

Source: Internet
Author: User

When String a = "abc"; is executed, the Java VM creates three char values 'A', 'B', and 'C' in the stack ', then create a String object in the heap. Its value is an array consisting of the three char values just created in the stack {'A', 'B ', 'C'}, and the newly created String object will be added to the String pool. If we execute String B = new String ("abc"); Code, because "abc" has been created and saved in the String pool, therefore, the Java Virtual Machine will only create a new String object in the heap, but its value (value) it shares the three char values 'A', 'B', and 'C' created in the stack during execution of the previous line of code '. String a = "abc"; an object is created in the String pool instead of in the heap. Therefore, only one object is created, and String B = new String ("abc "); there are objects in the String constant pool and heap, so they are two objects: String a = "abc"; new String ("abc") in the constant pool "); in the heap, String str1 = "abc"; the referenced object is in the stack (or the String pool. String str1 = new String ("abc"); the referenced object is in the memory/heap.

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.