Comparison between the null and "" equals square methods of the string class

Source: Internet
Author: User

Comparison between the null and "" equals square methods of the string class

'Statement

Public Shared Function Equals (_
A As String ,_
B As String ,_
ComparisonType As StringComparison _
) As Boolean
Parameters
A
Type: System. String
The first string to be compared, or Nothing.

B
Type: System. String
The second string to be compared, or Nothing.

ComparisonType
Type: System. StringComparison
One of the enumerated values, used to specify the comparison rules.

Return Value
Type: System. Boolean
If the value of parameter a is equal to the value of parameter B, true is used; otherwise, false is used.

String. the equals () method compares the content of a string and uses equals (...) the method compares all characters in the string one by one. If they are completely equal, true is returned. In this case, all strings are the same, so when the string s0 is compared with s1 or s2, the returned value is true. The operator "=" compares the String instance references. In this case, it is obvious that s0 and s1 are not the same String instance, but s0 and s2 are the same

Public class StringTest {
Public static void main (String [] args ){
String str = null;
String str2 = "";

System. out. println ("= str:" + "". equals (str ));
System. out. println ("= str2:" + "". equals (str2 ));

Try {
System. out. println (str. equals (""));
} Catch (NullPointerException e ){
System. err. println ("Caught Exception:" + e );
}
}
}


The running result is as follows:
"" = Str: false
"" = Str2: true
Caught Exception: java. lang. NullPointerException
In Java, String has many special features. We recommend that you use this method when comparing strings with null values:
"". Equals (str)


The following example uses the three versions of the Equals method to determine whether the String object is equal to the StringBuilder object.

 

Sample for String. Equals (Object)
'String. Equals (String)
'String. Equals (String, String)
Imports System
Imports System. Text

Class Sample
Public Shared Sub Main ()
Dim sb As New StringBuilder ("abcd ")
Dim str1 As [String] = "abcd"
Dim str2 As [String] = Nothing
Dim o2 As [Object] = Nothing

Console. WriteLine ()
Console. WriteLine ("* The value of String str1 is '{0}'.", str1)
Console. WriteLine ("* The value of StringBuilder sb is '{0}'.", sb. ToString ())

Console. WriteLine ()
Console. WriteLine ("1a) String. Equals (Object). Object is a StringBuilder, not a String .")
Console. WriteLine ("Is str1 equal to sb? : {0} ", str1.Equals (sb ))

Console. WriteLine ()
Console. WriteLine ("1b) String. Equals (Object). Object is a String .")
Str2 = sb. ToString ()
O2 = str2
Console. WriteLine ("* The value of Object o2 is '{0}'.", o2)
Console. WriteLine ("Is str1 equal to o2? : {0} ", str1.Equals (o2 ))

Console. WriteLine ()
Console. WriteLine ("2) String. Equals (String )")
Console. WriteLine ("* The value of String str2 is '{0}'.", str2)
Console. WriteLine ("Is str1 equal to str2? : {0} ", str1.Equals (str2 ))

Console. WriteLine ()
Console. WriteLine ("3) String. Equals (String, String )")
Console. WriteLine ("Is str1 equal to str2? : {0} ", [String]. Equals (str1, str2 ))
End Sub 'main
End Class 'sample'
'
'This example produces the following results:
'
'* The value of String str1 is 'abc '.
'* The value of StringBuilder sb is 'abcd '.
'
'1a) String. Equals (Object). Object is a StringBuilder, not a String.
Is str1 equal to sb? : False
'
'1b) String. Equals (Object). Object is a String.
'* The value of Object o2 is 'abc '.
Is str1 equal to o2? : True
'
'2) String. Equals (String)
'* The value of String str2 is 'abc '.
Is str1 equal to str2? : True
'
'3) String. Equals (String, String)
Is str1 equal to str2? : True

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.