Java (C #) underlying differences-string

Source: Internet
Author: User

1. String comparison

Java

Comparing a String object cannot simply use the comparison operator "= =" because the comparison operator compares the address of a two string . Even though the contents of the two strings are the same, the memory addresses of the two objects are different, using the comparison operator is still

will return false. You need to use equals or equalsignorecase.

Examples are as follows:

 Public Static voidMain (string[] args) {/** Comparisons of String objects cannot be done simply by using the comparison operator "= =" because the * comparison operator compares the addresses of two strings.     Even if the contents of the two strings are the same, and the memory addresses of the two objects are different, the use of the comparison operator will still return false. * Need to use equals or equalsignorecase*/String S1=NewString ("ABC"); String S2=NewString ("ABC"); Booleanb =s1.equals (S2); BooleanB2 =s1.equalsignorecase (S2); System.out.println ("Equal result:" +b); System.out.println ("Equalsignorecase Result:" +b2);}

The results of the operation are as follows:

C#

In C #, for reference types, the equals sign (= =) compares the "references" of two variables, that is, whether the referenced "address" is the same. For equals, the "content" of the variable is still the same, since string is a string class in the Microsoft package, internally he has overridden the = = operator. after rewriting, he compares the contents of the two variables.

Examples are as follows:

 Public Static void TestMethod () {    string"ABC";     string " ABC " ;     bool result = S1 = = S2;    Console.WriteLine (result);}

2. String formatting

Java and C # are mainly formatted keywords, different forms, more details, not detailed introduction.

Java (C #) underlying differences-string

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.