string-interning

String comparison and String interning in Java

北城余情 提交于 2019-11-26 13:47:38
When should one compare String s as objects and when should one use their equals method? To make sure, I always use equals , but that doesn't seem very efficient. In what situations can I be certain that string1 == string2 is a safe to use? Thanks! You should almost always use equals . You can be certain that string1 == string2 will work if: You've already made sure you've got distinct values in some other way (e.g. you're using string values fetched from a set, but comparing them for some other reason) You know you're dealing with compile-time string constants You've manually interned the

When to use intern() on String literals

限于喜欢 提交于 2019-11-26 12:39:27
问题 I see a lot of legacy code like this: class A { public static final String CONSTANT = \"value\".intern(); ... } I don\'t see any reason for the intern(), as in the Javadoc one can read: \"All literal strings and string-valued constant expressions are interned.\" Is there some intent of this, maybe in past revisions of the language? 回答1: This is a technique to ensure that CONSTANT is not actually a constant. When the Java compiler sees a reference to a final static primitive or String, it

When does python choose to intern a string [duplicate]

筅森魡賤 提交于 2019-11-26 12:17:57
问题 This question already has an answer here: About the changing id of an immutable string 5 answers >>> s1 = \"spam\" >>> s2 = \"spam\" >>> s1 is s2 True >>> q = \'asdalksdjfla;ksdjf;laksdjfals;kdfjasl;fjasdf\' >>> r = \'asdalksdjfla;ksdjf;laksdjfals;kdfjasl;fjasdf\' >>> q is r False How many characters should have to s1 is s2 give False ? Where is limit? I.e., I am asking how long a string has to be before python starts making separate copies of it. 回答1: String interning is implementation

java string concatenation and interning

给你一囗甜甜゛ 提交于 2019-11-26 09:59:21
问题 Question 1 String a1 = \"I Love\" + \" Java\"; String a2 = \"I Love \" + \"Java\"; System.out.println( a1 == a2 ); // true String b1 = \"I Love\"; b1 += \" Java\"; String b2 = \"I Love \"; b2 += \"Java\"; System.out.println( b1 == b2 ); // false In the first case, I understand that it is a concatenation of two string literals, so the result \"I Love Java\" will be interned, giving the result true. However, I\'m not sure about the second case. Question 2 String a1 = \"I Love\" + \" Java\"; //

Does Python intern strings?

丶灬走出姿态 提交于 2019-11-26 09:12:25
问题 In Java, explicitly declared Strings are interned by the JVM, so that subsequent declarations of the same String results in two pointers to the same String instance, rather than two separate (but identical) Strings. For example: public String baz() { String a = \"astring\"; return a; } public String bar() { String b = \"astring\" return b; } public void main() { String a = baz() String b = bar() assert(a == b) // passes } My question is, does CPython (or any other Python runtime) do the same

String comparison and String interning in Java

你说的曾经没有我的故事 提交于 2019-11-26 03:44:42
问题 When should one compare String s as objects and when should one use their equals method? To make sure, I always use equals , but that doesn\'t seem very efficient. In what situations can I be certain that string1 == string2 is a safe to use? Thanks! 回答1: You should almost always use equals . You can be certain that string1 == string2 will work if: You've already made sure you've got distinct values in some other way (e.g. you're using string values fetched from a set, but comparing them for

Do common JavaScript implementations use string interning?

你说的曾经没有我的故事 提交于 2019-11-26 00:38:25
问题 Do common JavaScript engines, such as V8 and WebKit\'s JavaScriptCore, use string interning for JavaScript strings? Or do they actually keep multiple instances of identical strings in memory? 回答1: Yes. In general any literal string, identifier, or other constant string in JS source is interned. However implementation details (exactly what is interned for instance) varies, as well as when the interning occurs. Note that a string value is not the same as a String Object though, String Objects

When should we use intern method of String on String literals

核能气质少年 提交于 2019-11-25 22:48:23
问题 According to String#intern(), intern method is supposed to return the String from the String pool if the String is found in String pool, otherwise a new string object will be added in String pool and the reference of this String is returned. So i tried this: String s1 = \"Rakesh\"; String s2 = \"Rakesh\"; String s3 = \"Rakesh\".intern(); if ( s1 == s2 ){ System.out.println(\"s1 and s2 are same\"); // 1. } if ( s1 == s3 ){ System.out.println(\"s1 and s3 are same\" ); // 2. } I was expecting

What is Java String interning?

浪子不回头ぞ 提交于 2019-11-25 21:53:54
问题 What is String Interning in Java, when I should use it, and why ? 回答1: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#intern() Basically doing String.intern() on a series of strings will ensure that all strings having same contents share same memory. So if you have list of names where 'john' appears 1000 times, by interning you ensure only one 'john' is actually allocated memory. This can be useful to reduce memory requirements of your program. But be aware that the cache is