Application analysis of String class in Java, double equal sign comparison string object

Source: Internet
Author: User

public class Test {
public static void Main (string[] args) {
String a = null;
String B = null;
String C = "";
String d = "";
String e = new String ("abc");
String f = "abc";

System.out.println ("a = = B:" + (A = = b));
System.out.println ("c = = d:" + (c = = d));
System.out.println ("E = = f:" + (E = = f));
}
}

Console output:

A = = B:true
c = = D:true
E = = F:false

The first A and B comparisons are null, so a is equal to B.

The second "" string is an anonymous object, and both C and D point to the anonymous object, and the heap memory address is the same, so C is equal to D.

The third e actually creates 2 string objects, one is an anonymous object of "ABC", one is a heap memory space object through the new keyword, and then e points to the string object created by new, and F is actually the anonymous object of "ABC". So f and e are not equal.

String g = new string (null); This method is not possible, the compile time will be error.

Application analysis of String class in Java, double equal sign comparison string object

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.