12.2. Initializing Strings

As stated earlier, the optimal way to initialize strings is by assigning a string reference to a string literal. Because of transparent optimizations performed by the runtime, it is imperative to know when two strings are equal. The semantics of equality for strings exist at different levels. Strings can be compared by comparing string instances, object instances, and their values. The default comparison operations in both C# and Java compare the strings' values instead of the object references. This is a reasonable feature considering that strings are commonly used, and because of interning, it makes little sense to compare strings as string instances or object instances. Listing 12.4 illustrates some of these semantic differences.

Listing 12.4. Initializing and Comparing C# Strings
using System;

public class StringTest {

  public static void Main(string[] args) {
    char[] c = new char[] {'H','e','l','l','o'};
    string[] strings = new string[4];
    strings[0] = "Hello";
    strings[1] = "World";
    strings[2] = "Hello";
    strings[3] = new string(c);

    Console.WriteLine("-----[ Comparing strings using ==
                     operator of System.String ]-----");
    for (int i =0; i < strings.Length; ++i) {
      for (int j = i+1; j < strings.Length; ++j) {
        Console.WriteLine("{0} = {1} {2}",i,j,strings[i] ==
                                                  strings[j]);
      }
    }
    Console.WriteLine("-----[ Comparing strings using ==
                     operator of System.Object ]-----");
    for (int i =0; i < strings.Length; ++i) {
      for (int j = i+1; j < strings.Length; ++j) {
          Console.WriteLine("{0} = {1}
             {2}",i,j,(object)strings[i] ==
               (object)strings[j]);
      }
    }
    Console.WriteLine("-----[ Comparing String using the Equals
                     method ]-----");
    for (int i =0; i < strings.Length; ++i) {
      for (int j = i+1; j < strings.Length; ++j) {
        Console.WriteLine("{0} = {1}
            {2}",i,j,strings[i].Equals(strings[j]));
      }
    }
  }
}

The output of Listing 12.4 is as follows:

Using == operator of System.String]
0=1 False
0=2 True
0=3 True
1=2 False
1=3 False
2=3 True
Using == operator of System.Object ]
0=1 False
0=2 True
0=3 False
1=2 False
1=3 False
2=3 False
Using the String.Equals method
0=1 False
0=2 True
0=3 True
1=2 False
1=3 False
2=3 True

In C#, the == operator of the System.Object method compares the references of the objects and returns true if the two references point to the same object, and false otherwise. The == operator is overloaded in the System.String class to compare the value of the strings instead of the actual objects they refer to. Therefore, in Listing 12.4, strings[3] == strings[0] returns true because they have the same string value. However, when both strings[0] and strings[3] are cast to the object and then compared using the == operator, they return false. This is because strings[3] points to a different string object.

C# also has the Equals method, which is similar in behavior to the == operator. Notice in the output of Listing 12.4 that comparing strings using the Equals method and the overloaded == operator gives the same results.

Java programmers must be particularly careful when dealing with the == operator and C# strings.

Note that in Java the equals method functions the same way as the == operator of C#. The equals method is defined on java.lang.Object, and by default it compares the object references. However, the java.lang.String class overrides the equals method and compares the String values. Both Java and C# support String interning, so both the JVM and the CLR create only one object from a String that has the same value. This is an optimization feature so that different String references pointing to the same literal value are internally stored as pointing to the same String object. The process of interning is transparent in both C# and Java.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.175.180