Source: https://blogs.msdn.microsoft.com/ericlippert/2009/09/28/string-interning-and-string-empty/
String interpretation is a compiler optimization method. If there are two identical string literals in one compiler, then the generated code ensures that there is only one string object (characters enclosed in double quotes) to build the entire instance of this literal.
I am from C # background, so I can explain by giving an example:
object obj = "Int32"; string str1 = "Int32"; string str2 = typeof(int).Name;
conclusion of the following comparisons:
Console.WriteLine(obj == str1); // true Console.WriteLine(str1 == str2); // true Console.WriteLine(obj == str2); // false !?
Note 1 . Objects are compared by reference.
Note2 : typeof (int). The name is evaluated by reflection, so it is not evaluated at compile time. Here, these comparisons are performed at compile time.
Analysis of the results: 1) true, because both of them contain the same literal, and therefore the generated code will have only one object referencing "Int32". See note 1 .
2) true, because the contents of both values โโare checked, which is the same.
3) FALSE because str2 and obj do not have the same literal. See Note 2 .
Robin gupta
source share