Java String class constant pool analysis and "equals" and "= =" Differences Detailed introduction _java

Source: Internet
Author: User
Tags format definition

Similarities and differences between Java "equals" and "= ="

First, simply say "equal" and "= ="

The = = Operation compares the values of two variables to the base data type for equality.

For reference variables, the two variables are stored in the heap in the same address,

Is that the contents of the stack are the same

Whether the two variables represented by the equals operation are references to the same object,

That is, the contents of the heap are the same.

In summary, = = Compare the address of 2 objects, while equals compares the contents of 2 objects.

Let's briefly introduce the string class

String classes are also called immutable character sequences

String uses private final char value[] to store the string, meaning that after the string object is created, the string content stored in this object cannot be modified. The string class has a special creation method, which is created by using the double quotation marks. For example, the new string ("123") actually creates 2 String objects, one is created by "123" through "double quotes, and the other is created by new." But they create different periods, one is the compilation period, the other is the runtime. Java has overloaded the + operator on string type, and can connect two strings directly using +. The runtime calls the Intern () method of the String class to add objects dynamically to string pool.

Distinguish between two methods for creating a String Object "" and New ()

A string is a special wrapper class data. Can be used:

String str1 = new String ("123");
String str2 = "123";

Two forms to create a

The first is to create a new object with new (), which is stored in the heap. Each time a call is made, a new object is created. (actually two, as noted above, but "123" in a constant pool will no longer create a new "123" in the constant pool)
The second is to create a reference variable str in the stack for the object of the string class, and then through the symbolic reference go to the string constant pool to find there is no "ABC", if not, then put "ABC" into the string constant pool, and make str point to "ABC", if there is already "ABC" Then direct str to "ABC".

Then we should be aware

On the one hand, the first method is advantageous and saves memory space. At the same time it can improve the speed of the program to some extent, because the JVM automatically determines whether it is necessary to create new objects based on the actual situation of the data in the stack. For code with string str = new String ("123"), all the new objects are created in the heap, regardless of whether their string values are equal or not, and it is necessary to create new objects, which increases the burden on the program. On the other hand, when we use the format definition class such as string str = "123", we always think of course that the object str of the string class was created.
The object may not have been created! Instead, you might just point to an object that you have previously created. Only the new () method can be used to guarantee that one object is created each time.

Take a look at the following examples

Package teststring; 
    public class TestString {public static void main (string[] args) {String a = "123"; 
    String B = "123"; 
    System.out.println (A==B); 
    System.out.println (A.equals (b)); 
    System.out.println ("------------------------------------------"); 
     /** * true * true * This creates a string "123" stored in a constant pool * because "123" is stored in a constant area, and the only, two string references A and B have the same address so a==b is true 
    * and two references are identical in the contents of the object in the heap so A.equals (b) is true */String c = new string ("1234"); 
    String d = new string ("1234"); 
    System.out.println (C==d); 
    System.out.println (C.equals (d)); 
    System.out.println ("------------------------------------------"); * * False * true * This creates three string "1234", one in a constant pool, two in the heap via new * because C and D are pointing to two string objects in the heap at this point, so the address is different c==d to FA 
    LSE * But C and the contents of the D heap are the same so C.equals (d) is true/String e = "A1"; 
    String f = "a" +1; 
    System.out.println (E==F); 
    System.out.println (E.equals (f)); System.out.println ("------------------------------------------"); 
     /** * true * true * this creates "A1" "a" 2 strings where "A1" "a" they both are in a constant pool, you might ask + is an operator overload? 
     * Yes, Java itself has certain operator overloads but you can't use the operator overload that defines itself, unlike C + +, String f = "a" +1; 
     * This sentence will be made into a String f= "A1" by the compiler, which is the same as the first case we talked about and no longer repeat. 
    * The compiler did so because he was able to determine at compile time */String g = "GH"; 
    String hh = "H"; 
    String h = "g" + hh; 
    System.out.println (G==H); 
    System.out.println (G.equals (h)); 
    System.out.println ("------------------------------------------"); /** * False * true * Unlike the above, H is not determined at compile time (the compiler thinks so), so the object H refers to is stored in the heap at runtime, * So G==h is true and G.equals (h) is False */}}

Thank you for reading, I hope to help you, thank you for your support for this site!

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.