Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Wanna Share an Interesting Tricky Code Question of String Intern()

 
Ranch Foreman
Posts: 122
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



 
Marshal
Posts: 70684
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What a cheek, asking for +1s

I got false false true. How on earth did the third equality evaluate to true? Is that a good interview question? Or is it simply a party trick, like, “how can you run code with no main method?”
 
Rancher
Posts: 218
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran this, and got false - true - true.

This returns true:

And this:

Returns false - false - false.

Something to do with the Java String Pool internals and which Strings are interned by default, I'd guess.

I wouldn't quite say this is a good interview question - detailed knowledge of the Java String Pool internals are probably not too relevant to real career work.
 
Master Rancher
Posts: 3701
44
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Mike Simmons
Master Rancher
Posts: 3701
44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
Sheriff
Posts: 15942
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Frank Mi
Ranch Foreman
Posts: 122
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.  
 
Campbell Ritchie
Marshal
Posts: 70684
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank goodness for that. I did mention another bad question earlier: “how can you run code with no main method?” Any ideas?
 
Marshal
Posts: 7791
536
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:“how can you run code with no main method?” Any ideas?


Sure. Your favourite JShell?
 
Campbell Ritchie
Marshal
Posts: 70684
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I only have un‑favourite JShells What if you want to do it without JShell. And keep quiet, Liutauras, because I know you know the answer already..
 
Frank Mi
Ranch Foreman
Posts: 122
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I did mention another bad question earlier: “how can you run code with no main method?” Any ideas?



I do remember I read it somewhere when I just began learning Java that I could use a static block instead of main method for the trick. However, it doesn't work now. Is it fixed by Java?

 
Campbell Ritchie
Marshal
Posts: 70684
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Frank Mi wrote:. . . static block . . .

Its official name is a static initialiser.

However, it doesn't work now. Is it fixed by Java? . . .

It used to work but (I think) in Java7, (I think) they altered the start up process which seems to look for the main method before the class is loaded and throws an exception.
 
Frank Mi
Ranch Foreman
Posts: 122
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Frank Mi wrote:. . . static block . . .

Its official name is a static initialiser.



Forgive me, when I first learned java, that book was not even written in English.

Campbell Ritchie wrote:

Frank Mi wrote:However, it doesn't work now. Is it fixed by Java? . . .

It used to work but (I think) in Java7, (I think) they altered the start up process which seems to look for the main method before the class is loaded and throws an exception.



Then I give JShell another vote up.  
 
Mike Simmons
Master Rancher
Posts: 3701
44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Frank Mi wrote:. . . static block . . .

Its official name is a static initialiser.


Well, its official name is actually static initializer. :p ;)
 
Mike Simmons
Master Rancher
Posts: 3701
44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic