"Java Heavy Difficulty Knowledge" string S=new string ("abc") and string= "ABC"; Difference

Source: Internet
Author: User

First, String s=new string ("abc"); The sentence creates 2 objects;

One instance object for new and the other is "ABC";

S is not an object, S is just a reference (a pointer in C + +).

To query the constructor of the string class in Java, there is one such constructor with the following API:

Public (original)
    • Initializes a newly created String object to represent a sequence of characters that are the same as the argument; in other words, the newly created string is a copy of the parameter string. Because the String is immutable, you do not need to use this construction method unless you want original an explicit copy.

      • Parameters:

      • original-A String .

The string s= "abc" apparently created only one object.

String s= "A" + "B"; This sentence creates 3 objects, "a" "B" and "AB"

Secondly, the difference between the two is as follows:

String S=new string ("abc"); The resulting object will not be in the JVM (Java Virtual machine) Chang (precompiled)

and the latter is. (Note: The String class is a final type and the String class object cannot be modified.)

Reference: http://www.cnblogs.com/ydpvictor/archive/2012/09/09/2677260.html

"Java Heavy Difficulty Knowledge" string S=new string ("abc") and string= "ABC"; Difference

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.