Java Fundamentals Review for string objects, you can use the "=" assignment, or you can use the New keyword to assign a value, what is the difference between the two ways?

Source: Internet
Author: User
The string type is a type that is often used in the actual work, divided from the data type, string is a reference type, and is a class defined in the API. So objects of type string can be created with new, such as String Name=new string ("string"), assigned to the variable name, the value "string", but the string analogy to other classes is somewhat special, you can use ' = ' to directly assign values, such as String Name= "string", which is also assigned to the variable name, the value "string", the two assignments are different, using new assignment, always creates a new object, initializes the value of the string in the new memory space, and uses "=" To assign the value, Instead of initializing a new string each time, it looks for a string from a "string instance pool" with no strings to be assigned, if there is a direct reference, if it does not exist, initializes a string and puts it into the string instance pool. In practical programming, "=" is often used to assign a value to a variable of type string.

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.