Java Puzzlers: Scraping the Bottom of the Barrel
Recorded at:
I love Java Puzzlers!
by
Steve McJones
A few years ago I was looking for a better language to develop on the JVM and I found Scala.
I decided to got through the Java Puzzlers book one-by-one and find out if Scala repeats the mistakes. It seems the creators of the language did exactly the same when designing it. Most of them are just fixed and work the way they should.
A few of them were sadly not fixed, because of compatibility with Java (Like the implicit widening conversion from integers to floating point numbers or the weird Unicode escaping behavior in source code comments). But overall it is great to see such a concentration on detail in a programming language.
First puzzler (using Scala's Seq and List, with and without type parameters):
Works. Prints "123".
Second puzzler:
Works. BigDecimal(2.00) - BigDecimal(1.10) // result: scala.math.BigDecimal = 0.9.
Third puzzler (using java.util.List/ArrayList):
Works. Prints "[null]".
Fourth puzzler:
Same in Scala. Nothing can be done here, as mentioned in the talk.
Fifth puzzler:
Works.
Sixth puzzler:
Same in Scala (Java compat.). Classic integer overflow.
Seventh puzzler:
Same in Scala (Java compat.). Problem is using a font where l and 1 look the same.
Quite nice!
Re: I love Java Puzzlers!
by
Alex Miller
strangeloop2010.com/talks/14450
Re: I love Java Puzzlers!
by
Roxana Bacila
Re: I love Java Puzzlers!
by
Morgan Creighton
However, now that I've seen puzzler #2 and the explanation, I'm actually troubled that Scala's BigDecimal "works". Why did Scala round the double 1.1 to exactly 11/10, which is not representable exactly in floating point? That behavior feels like a bug to me. Steve, are you sure your test displays enough significant digits?
In any case, I'd much rather not have a constructor (actually it's an apply method on the companion object) that accepts doubles at all, as Bob and Josh suggested.
perl regexp
by
Evgeny Filippov
brilliant talk
by
Sougata Som
One comment regarding the last puzzler - I think one of the morals should be to change the font of your IDE to something that clearly distinguishes between lowercase L and numeral 1.




Hello stranger!
You need to Register an InfoQ account or Login to post comments. But there's so much more behind being registered.Get the most out of the InfoQ experience.
Tell us what you think