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 ...

Get .NET for Java Developers: Migrating to C# now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.