string-interning

intern() behaving differently in Java 6 and Java 7

回眸只為那壹抹淺笑 提交于 2019-12-17 07:10:13
问题 class Test { public static void main(String...args) { String s1 = "Good"; s1 = s1 + "morning"; System.out.println(s1.intern()); String s2 = "Goodmorning"; if (s1 == s2) { System.out.println("both are equal"); } } } This code produces different outputs in Java 6 and Java 7. In Java 6 the s1==s2 condition returns false and in Java 7 the s1==s2 returns true . Why? Why does this program produces different output in Java 6 and Java 7? 回答1: It seems that JDK7 process intern in a different way as

String interning in .Net Framework - What are the benefits and when to use interning

孤街浪徒 提交于 2019-12-17 05:04:50
问题 I want to know the process and internals of string interning specific to .Net framework . Would also like to know the benefits of using interning and the scenarios/situations where we should use string interning to improve the performance. Though I have studied interning from the Jeffery Richter's CLR book but I am still confused and would like to know it in more detail. [Editing] to ask a specific question with a sample code as below: private void MethodA() { string s = "String"; // line 1 -

Why does this string have a reference count of 4? (Delphi 2007)

二次信任 提交于 2019-12-12 09:30:21
问题 This is a very Delphi specific question (maybe even Delphi 2007 specific). I am currently writing a simple StringPool class for interning strings. As a good little coder I also added unit tests and found something that baffled me. This is the code for interning: function TStringPool.Intern(const _s: string): string; var Idx: Integer; begin if FList.Find(_s, Idx) then Result := FList[Idx] else begin Result := _s; if FMakeStringsUnique then UniqueString(Result); FList.Add(Result); end; end;

Are .NET resource file strings interned?

自古美人都是妖i 提交于 2019-12-11 00:02:16
问题 When I use a .resx file to store fixed string values (to be bound to controls on an .aspx page), are these strings interned? I presume the compiler reads in the strings from the XML file and replaces them as literals in the code, and therefore they become interned. Is this correct? 回答1: @Marc- good point. I tried it and the answer is, 'no', they're not interned, at least for the GetGlobalResourceObject() method, so I assume the same for local resources. So, now I wonder why. Resources are

In Python, why do separate dictionary string values pass “in” equality checks? ( string Interning Experiment )

自闭症网瘾萝莉.ら 提交于 2019-12-10 14:59:31
问题 I am building a Python utility that will involve mapping integers to word strings, where many integers might map to the same string. From my understanding, Python interns short strings and most hard-coded strings by default, saving memory overhead as a result by keeping a "canonical" version of the string in a table. I thought that I could benefit from this by interning string values, even though string interning is built more for key hashing optimization. I wrote a quick test that checks

What are the rules for cpython's string interning?

北城余情 提交于 2019-12-09 00:34:18
问题 In python 3.5, is it possible to predict when we will get an interned string or when we will get a copy? After reading a few Stack Overflow answers on this issue I've found this one the most helpful but still not comprehensive. Than I looked at Python docs, but the interning is not guaranteed by default Normally , the names used in Python programs are automatically interned, and the dictionaries used to hold module, class or instance attributes have interned keys. So, my question is about

Operator overloading in Generic Methods

给你一囗甜甜゛ 提交于 2019-12-07 03:39:57
问题 This code snippet is from C# in Depth static bool AreReferencesEqual<T>(T first, T second) where T : class { return first == second; } static void Main() { string name = "Jon"; string intro1 = "My name is " + name; string intro2 = "My name is " + name; Console.WriteLine(intro1 == intro2); Console.WriteLine(AreReferencesEqual(intro1, intro2)); } The output of the above code snippet is True False When the main method is changed to static void Main() { string intro1 = "My name is Jon"; string

Shouldn't I do `String s = new String(“a new string”);` in Java, even with automatic string interning?

别来无恙 提交于 2019-12-07 02:56:49
问题 Ok, this question is an extension of this question Java Strings: "String s = new String("silly");" The above question asked the same question as this one, but I have a new doubting point. According to Effective Java and the answers of above question, we should not do String s = new String("a new string"); , because that will create unnecessary object. I am not sure about this conclusion, because I think Java is doing automatic string interning, which means for a string, anyway there is only

Why are annotation string values not interned?

房东的猫 提交于 2019-12-06 16:59:06
问题 The following snippet prints 4 distinct hash codes, despite reusing a string constant and literal. Why are string values not interned on annotation elements? public class Foo { @Retention(RetentionPolicy.RUNTIME) @interface Bar { String CONSTANT = "foo"; String value() default CONSTANT; } public static void main(String[] args) throws Exception { System.out.println(System.identityHashCode(Bar.CONSTANT)); System.out.println(System.identityHashCode(Foo.class.getMethod("test1").getAnnotation(Bar

Search cost of string interning and declaration of literal strings

烈酒焚心 提交于 2019-12-05 06:19:40
Two Questions. When we declare literal strings, we search whether there is the same string in string pool of heap. Is this also an interning (method intern of class String )? In my thought, each literal string declaration needs a binary search or something so it costs at least log(n) when n is number of existing strings in the pool. And if there are many strings in the pool, it may be high cost. (maybe tradeoff of searching cost and memory?) On this point of view, it might be dangerous to declare mant literal strings. How significant is this searching cost and why java is designed in this way