String in C/C++, C#, and Java.

A string, which is composed of a sequence of characters, is quite an important data structure. Many programming languages have special rules for strings for the optimization purpose, some of which are highlighted in C/C++, C#, and Java
 Strings in C/C++ 
All literal strings in C/C++ end with a special character ‘\0’, so it is easy to find the end of a string. However, there is an extra cost for the special character, and it is easy to make mistakes. 

C/C++ Code for End of a String
char str[10];
strcpy(str, "0123456789");
A character array with length 10 is declared first, and the content of a string “0123456789” is copied into it. It seems that the string “0123456789” only has 10 characters, but its actual length is 11 because there is an extra character ‘\0’ at its end. The length should be at least 11 for a character array to accommodate the string.
Strings in C#
Strings are encapsulated in a class System.String in C#, whose contents are immutable. When we try to modify the content of a string, a new instance will be created. There are many interview questions about immutable strings. For example,
C# Code for Immutable Strings
String str = "hello";
str.Insert(0, " WORLD");
Although there are two operations, ToUpper and Insert, on str, it keeps the original content “hello” unchanged. When we try to modify the content of a string, the modified result is in return value.
If there are multiple editing operations on a string, multiple temporary instances will be created, and it has a negative impact on both time and space efficiencies. A new class related to string, StringBuilder, is defined to accommodate the modified result. Usually, StringBuilder is a better choice if we continue modifying strings many times.
Similar to editing strings, a new instance will be created when we try to assign a literal string to another string. An example is shown inbelow

C# Code to Assign Strings
void ValueOrReference(Type type) {
    String result = "The type " + type.Name;
    if (type.IsValueType)
        Console.WriteLine(result + " is a value type.");
        Console.WriteLine(result + " is a reference type.");
void ModifyString(String text) {
    text = "world";
void Main(string[] args) {
    String text = "hello";
This example checks whether the class String is a value type and reference type first. Since it is defined as public sealed class String {...}, it is a reference type.
It assigns a new string “world” to text in the method ModifyString. A new string instance with content “world” is created here, and it is referenced by text. The variable text references the original string outside the method ModifyString because text is not marked as ref or out in the argument list. Therefore, the output for text is still “hello”. If the expected output is “world”, we have to mark the parameter text with ref or out.
Strings in Java
A section of memory is allocated for literal strings in Java. When a string is created once, it can be referenced by other instances. This optimization mechanism avoids recreation, but it makes identity and equality tests more complicated and confusing.
 Java Code for Equality and Identity of Strings
void testEquality() {
    String str1 = "Hello world.";
    String str2 = "Hello world.";
    if (str1 == str2)
        System.out.print("str1 == str2\n");
        System.out.print("str1 != str2\n");
        System.out.print("str1 equals to str2\n");
        System.out.print("str1 doesn't equal to str2\n");
    String str3 = new String("Hello world.");
    String str4 = new String("Hello world.");
    if (str3 == str4)
        System.out.print("str3 == str4\n");
        System.out.print("str3 != str4\n");
        System.out.print("str3 equals to str4\n");
        System.out.print("str3 doesn't equal to str4\n");

When the first line of code String str1 = “Hello world.” executes, a string “Hello world.” is created, and the variable str1 references to it. Another string “Hello world.” will not be created again when the next line of code executes because of optimization. The variable str2 also references the existing “Hello world.”.
The operator == checks the identity of the two objects (whether two variables reference the same object). Since str1 and str2 reference the same string in memory, they are identical to each other. The method equals checks equality of two objects (whether two objects have the same content). Of course, the contents of str1 and str2 are the same.
When code String str3 = new String(“Hello world.”) executes, a new instance of String with content “Hello world.” is created and it is referenced by the variable str3. Then another instance of String with content “Hello world.” is created again, and referenced by str4. Since str3 and str4 reference two different instances, they are not identical, but their contents are the same.
Therefore, the output contains four lines:
str1 == str2
str1 equals to str2
str3 != str4
str3 equals to str4 appreciates your contribution please mail us the questions you have to so that it will be useful to our job search community

No comments: