I guess string interning is very familiar to you guys. Let's do a test. What do you think the following codes would print? Please try to give an answer before running the code. Give me a vote up if you get it wrong (please don't vote down if you get it right).
Tip: your running result may be different by Java version, say java 5 or java 8, Java SE or openJDK. They are all correct, not bugs of eclipse. If you would like, you can also try how many different results you can get (then you have a good interview question to fence out most of your interviewees).
Update: please note that the good interview part in the tip is a joke (not very funny), please don't use it in an interview.
Campbell Ritchie wrote:How on earth did the third equality evaluate to true?
Basically, because "main" and (in some environments) "java" were already in the string pool. But "ranch" was not. So for the first two, intern() returns with a reference to an already-pooled instance, which is different from the one constructed in Frank's code. But for "ranch", there is no already-pooled instance, so intern() uses the "this" instance as the one to pool. So we get true, because the just-constructed "ranch" instance becomes the pooled instance.
Campbell Ritchie wrote:Is that a good interview question?
I don't think it is, really. More of a fun trivia question for those of us who enjoy that sort of thing. But I'd hate to exclude anyone from a job based on that. And, I'd be pretty suspicious of anyone who wrote code that depended on that, for professional work.
posted 3 weeks ago
"main" always has to be in the pool, because it's the name of the required "main" method, and method names are pooled. "ranch" is probably not in the pool, unless you do something to put it there. "java" seems to be the one that's dependent on your JVM and environment - I don't think it's required to be pooled, but I'm not surprised it's there, for some JVMs.
And when Zachary does this:
That guarantees that all three literals were in the string pool, before the three new String() constructors were pooled. Therefore intern() always returns the already-pooled instance, instead of the newly-constructed one.
Frank Mi wrote:If you would like, you can also try how many different results you can get (then you have a good interview question to fence out most of your interviewees).
No, but I would say interviewees might want to avoid any company that screens people based on questions like this. Seriously, this is not a good interview question unless your company's business is esoteric Java trivia.
The best ideas are the crazy ones. If you have a crazy idea and it works, it's really valuable.—Kent Beck
I said "good interview question" just for joking. I didn't expect you guys really want to get a good interview question.
The third one is a regular interview question about how the latest regular string interning works, not something strange. The other two are only used for tricking people, please don't use them in an interview of course. The "java" one is interesting because it has different result on Java SE and openJDK, so it has potential to trick even more people.
posted 3 weeks ago
Thank goodness for that. I did mention another bad question earlier: “how can you run code with no main method?” Any ideas?
As for using a static initializer without main, I think there was also a period (or at least a JVM version) where it would run the static initializer, then print an error message. So if you were playing code golf (minimizing total chars in your program) you had to check the problem statement carefully to see if it was OK to print the correct answer followed immediately by an error message.
Could you hold this kitten for a sec? I need to adjust this tiny ad: