O'Reilly logo

Scala Puzzlers by Nermin Šerifovic, Andrew Phillips

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Puzzler 22

Cast Away

Although all values in Scala are objects, its basic value types (Byte, Short, Int, etc.) are compiled where possible into their primitive counterparts in Java. This allows you to think of instances of those types as regular objects, simplifying the programming model. Treating Java primitives as Scala value types also makes working with Java libraries easier.

By contrast, no similar translation exists between Java and Scala collection types—you have to convert between them. Scala provides two objects, JavaConversions and JavaConverters, to help you deal with such conversions. JavaConverters is usually preferred, because it makes the conversions more obvious in the code.

The following program showcases the use of a Java collection ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required