Chapter 11. Interoperability between Scala and Java
This chapter covers
- Using Java classes in Scala
- Working with Java generics and collections
- Solving integration challenges
- Building web applications using Java frameworks
One of the most exciting features of Scala is that it runs on the JVM. The benefit of running on a JVM is that you can take advantage of all the frameworks and tools built into other JVM languages. More companies are moving to the JVM even if they don’t use Java as their primary programming language. I strongly believe that any language that doesn’t have support for the JVM is almost a nonstarter, for most of the software projects in the enterprise.
One of the main design goals of Scala is to run on a JVM and provide interoperability with Java. Scala is compiled to Java bytecodes, and you can use tools like javap (Java class file disassembler) to disassemble bytecodes generated by the Scala compiler. In most cases, Scala features are translated to Java features so that Scala can easily integrate with Java. For example, Scala uses type erasure[1] to be compatible with Java. Type erasure also allows Scala to be easily integrated with dynamically typed languages for the JVM. Some Scala features (such as traits) don’t directly map to Java, and in those cases you have to use workarounds (more about this in section 11.3).
1 Java Tutorials: Type erasure, http://download.oracle.com/javase/tutorial/java/generics/erasure.html.