The difference between string direct assignments in Java and C # and the use of new creation (= = vs. equals)

Source: Internet
Author: User

In Java, strings can be assigned directly or created using new, The direct assignment is that the string value is placed in the constant pool in the compile phase (. class file), and the memory space is no longer allocated if other variables are directly assigned to the same value, but instead it is given a reference address, whereas using new to create the space that is allocated when the program is run is a new reference address that can be inter The () method adds a string to the constant pool, and returns its reference if the string already exists in the constant pool. In the string class, = = is the same as the reference address for comparing two strings, and equals compares the values of both (= = and equals in other reference classes). Take a look at an example to deepen your understanding:

  1. String s1="sa";
  2. String s2="sa"; Make S1 and S2 point to the same reference address at compile time
  3. String s3=new String ("sa"); A new reference address is given to S3 when the program is run
  4. String s4=new String ("sa"). Intern (); When the program runs, it returns the reference address pointed to by S1 and S2 to S4.
  5. System.out.println (S1==S2);
  6. System.out.println (s1.equals (S2));
  7. System.out.println (S1==S3);
  8. System.out.println (S1.equals (S3));
  9. System.out.println (S1==S4);
  10. System.out.println (S1.equals (S4));
  11. System.out.println (S3==S4);
  12. System.out.println (S3.equals (S4));

Output Result:

    1. True
    2. True
    3. False
    4. True
    5. True
    6. True
    7. False
    8. True

The memory allocation mechanism for C # is not well understood, the difference between = = and equals in the string class is verified by the program, and you can see that both = = and equals are comparison values equal (in other reference classes = = Default comparison reference address, equals default comparison value):

    1. string S1 = "SB";
    2. String s2 = "SB";
    3. char[] t={'s ',' B '};
    4. String s3 = new String (t);
    5. Console.WriteLine (S1 = = s2);
    6. Console.WriteLine (S1. Equals (S2));
    7. Console.WriteLine (S1 = = S3);
    8. Console.WriteLine (S1. Equals (S3));

Output Result:

    1. True
    2. True
    3. True
    4. True


Please also point out if there is any mistake in the summary.

The difference between string direct assignments in Java and C # and the use of new creation (= = vs. equals)

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.