The differences between string two different creation methods in Java and the use of intern

Source: Internet
Author: User

One

Java has two ways of creating strings,

        String str1 = "abc";         New String ("abc");

Create with double quotes and create with new. Both of these methods are created differently on the location of the string store. When using double quotation marks, it is equivalent to explicitly declaring the value of a string (literal), so it is stored as a constant in a constant pool in the method area. When you use new to create a string, the JVM allocates an area on the heap, storing a string object with a value of "ABC".

Two

String = = and equals are different, = = Compares two strings in memory for the same address, and equals compares two string values. So, if the string is created in double quotation marks, then two strings point to the same position in the constant pool, then = = is established and equals is established. If the string is created in new mode, the two string is not true if it is not pointing to the same string object on the heap, and equals is true if the value is the same. Such as:

String str1 = "abc"; String str2=NewString ("ABC"); String STR3= "ABC"; String STR4=str2; System.out.println (str1= = STR3);//trueSystem.out.println (Str1.equals (STR3));//trueSystem.out.println (str2 = = STR3);//falseSystem.out.println (Str2.equals (STR3));//trueSystem.out.println (str2 = = STR4);//true, STR4 and str2 point to the same objectSystem.out.println (Str2.equals (STR4));//true

Three

A string created in two different ways, the result is different when the operation is performed. If a string is obtained from two constant operations, it is equivalent to creating it in double quotes, which is stored in a constant pool, such as:

        String s = "a" + "B";         = "AB";         // the above two methods of creation are completely equivalent

When an operation is made, a "operand" to the right of the equal sign is not constant (even if the operand is a constant created with double quotation marks), the resulting result is a new string object created by new, such as:

        String str1 = "abc";         = "ABCD";         = "ABC" + "D";         = str1 + "d";                 = = STR3); // true        System.out.println (Str2.equals (STR3)); // true        System.out.println (str2 = = STR4); // false        System.out.println (Str2.equals (STR4)); // true

Four, the use of the Intern () method. The Intern method returns a constant value that corresponds to a string. When the Intern method is executed, the JVM checks to see if there is a constant value in the constant pool that is the same as the string, returns the constant value if there is one, and returns the constant value if it does not. That is, intern returns a string in the value constant pool, not a string on the heap, which is equivalent to creating a string in double quotation marks.

        String str1 = "abc";         = "ABCD";         = "ABC" + "D";         = (str1 + "D"). Intern ();                 = = STR3); // true        System.out.println (Str2.equals (STR3)); // true        System.out.println (str2 = = STR4); // true        System.out.println (Str2.equals (STR4)); // true      // Note the difference from the above example

The differences between string two different creation methods in Java and the use of intern

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.