Java NULL String

Source: Internet
Author: User

In Java, the String would have different usage.

Example:

public class Test {public   static void Main (string[] args) {String S1 = "ACCP"; String s2 = "ACCP"; String s3 = new String (S1), if (S1 = = s2) {System.out.print ("true,");} else {System.out.print ("false,");}      if (S1 = = S3) {System.out.println ("true"),} else {System.out.println ("false");}}}   /* Output:true, false*/

For statement String S1 = "ACCP";
Inside Java, this statement is translated into the following steps:

    1. First define an object reference variable named S1 to the String class: string S1;
    2. Look in the stack for an address with a value of "ACCP".
      If not, open an address with a literal value of "ACCP", create a new object o for the string class, point the string value of O to the address, and note the referenced object o next to this address in the stack.
      If you already have an address with a value of "ACCP", look for the object o and return the address of O.
      So when the value of the variable that you want to create is present in the stack, it is actually referencing the address in the stack that already exists, not the new one.
    3. Point S1 to the address of the object o.

String S1 = "ACCP"; Its string value is a reference to the data in the existing stack! So the reference address of S1 and S2 is the same, and the S1==S2 is true.

and string s3 = new String (S1); is to create an object, that is, to create a new reference, so the S3 reference address is newly created, and of course the reference address of the S1 is different. So S1 = = S3 is false.

= = is the reference address of the comparison object, if you want to compare the value of the object with equal!

Java NULL String

Related Article

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.