5 ms·
Every time I see String.intern() my mind leaps to the problem of new Java programmers who are misled into this: String a = "hello"; String b = "world";
by Terr_ 10y ago
Every time I see String.intern() my mind leaps to the problem of new Java programmers who are misled into this:
String a = "hello";
String b = "world";
assert a != b;
b = "hello";
assert a == b;
// OH NICE I'LL USE == FOR STRING COMPARISONS NOW
It works cause source-code literals are intern'ed down into identical objects by the compiler, but that's a special case that won't apply to strings created at runtime.
- userbinator 10y agonew Java programmers Or perhaps interns? ;-P In fact, I've wondered about the origins of the term "intern" since I first heard of it; the technique was already known to me and likely others as "tokenising" before that, however, since compilers would often have to compare identifiers and doing this to them made it much faster.
- wtetzner 10y ago> In fact, I've wondered about the origins of the term "intern" since I first heard of it I always assumed it came from internalize, or something like that.
- derefr 10y agoI was curious, but a good amount of Googling didn't turn up any etymology. I'll take a guess, though, that it's from Common Lisp, because CL's intern function (http://clhs.lisp.se/Body/f_intern.htm http://clhs.lisp.se/Body/f_intern.htm) is clearly part of a suite of names (intern, unintern, :internal, :external) where, in context, "intern" makes complete sense as the name for the function.
- smarks 10y agoUsage of "intern" in this sense is much older than Common Lisp. The earliest reference I could find in a few minutes of searching is from 1974, from the MACLISP Reference Manual: http://www.softwarepreservation.org/projects/LISP/MIT/Moon-MACLISP_Reference_Manual-Apr_08_1974.pdf http://www.softwarepreservation.org/projects/LISP/MIT/Moon-M... See sections 6.3 and 6.4 and the glossary for definitions and uses of the word. Unfortunately they don't shed any light on where the term "intern" comes from. Typical usage is that a string is «"interned" or registered in an obarray» but it doesn't explain much more than that. Interning occurs most often at read time, when a string read from the input is turned into an atom, so I'd guess that "intern" is short for "internalize" or similar.
- derefr 10y agoWell, I was about to say, the Common Lisp version makes enough sense (and the MacLisp version might be equivalent, for all I know): In CL, you don't really have a global "symbol table" or "atom table"; instead, interning is about the runtime lexical scopes under which code compilation happens, which consist of stacks of imported namespaces ("packages"), where each namespace exists as a mutable object holding identifier "slots". (intern foo) is a mutation on the current namespace that creates a local variable "slot" for the identifier foo, shadowing any other foo that might have been in lexical scope from an import. That is, (intern foo) makes foo resolve internally to the current namespace. (unintern foo) is the complementary mutation; it drops the foo identifier from the current namespace, allowing other symbols (slots in packages) that were previously shadowed by it to re-appear.
- jsmthrowaway 10y agoInternment, perhaps?
- tyingq 10y agoI believe the original term was for recent medical grads working at a hospital before they had their license. It was "okay" because they were internal, and supervised. Once they had the license, they could work unsupervised or externally.
- brudgers 10y agoOED: †1. intr. To enter or pass in; to be come incorporated or united with another being. Obs. 2. trans. To confine within the limits of a country, district, or place; to oblige to reside within prescribed limits without permission to leave them.
- deleted 10y ago[deleted]
- josteink 10y ago> In fact, I've wondered about the origins of the term "intern" since I first heard of it; the technique was already known to me and likely others as "tokenising" before that, however, since compilers would often have to compare identifiers and doing this to them made it much faster. My first hearing of it was in conjunction with LISP macros, or more specifically creating hygenic LISP macros. I'm pretty sure it goes way back. You can see an example of this here: http://stackoverflow.com/a/30016344 http://stackoverflow.com/a/30016344
- lispm 10y ago'intern'ing in the Lisp world usually means to create a new object and put it under its name into some datastructure for later retrieval, unless the object already exists -> then the object is just retrieved. The typical example is to intern a symbol into a symbol table (list, array, or in Common Lisp into a package). Here the symbol table is searched for a symbol with the required name. If it exists, the symbol will be returned. If it does not exist, the symbol will be created and registered in the symbol table. But one might also use the word 'intern' for an operation to retrieve/put a user into a group, retrieve/put a resource into a pool, or similar operations. The function INTERN dates back at least to Lisp 1.5 from 1962. See the Lisp 1.5 Programmer's Manual.
- TeMPOraL 10y agoIt's from Lisp, and where did Lisp take it from in the sixties, I don't know. But I'll bet that this particular Java method comes straight from Lisp, as there were some Lisp folks involved in making Java (in particular, Guy Steele worked on both Common Lisp ANSI standard and Java language specification).
- pjmlp 10y agoAnd was one of the Scheme creators.
- swsieber 10y agoI ran into this once when I accidentally used == for a comparison to an empty string... and the condition went through part of the time because sometimes the empty string it was being compared through was also a constant....
- bbcbasic 10y agonew Java programmers, especially those coming from C# where == compares the strings.
- cousin_it 10y agoI think the problem is that == compares pointers, when it should've been sugar for .equals() instead.
- logicallee 10y agoit's tough. If a language has pointers, then you would expect == to compare whether the two sides have the same value (an address). I don't see a way around that. How would you expect a programmer to ask "do these two pointers hold the same address?" .holdsSameAddressAs()?
- _pmf_ 10y ago> How would you expect a programmer to ask "do these two pointers hold the same address?" .holdsSameAddressAs()? In C++, yes, you do (&a == &b) and the plain == is used for the common case of actually doing what you mean (what in Java would be .equals()).
- sherincall 10y agoActually, that's not always correct in C++ (it is in C). It is possible that either type has the operator& overloaded, so instead the proper way to do it is: (std::addressof(a) == std::addressof(b)) http://en.cppreference.com/w/cpp/memory/addressof http://en.cppreference.com/w/cpp/memory/addressof
- _pmf_ 10y agoThanks (I didn't even know this operator was overloadable)!
- HappyTypist 10y ago==& ? I use .equals far more than ==.
- tveita 10y agoD and Python use the "is" operator to compare reference identity. It is the lesser used operator and the more advanced concept, so using == for reference comparison is just a newbie trap, doubly so since you have to use == to compare equality for primitives.