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 ...

Get Scala Puzzlers now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.