Chapter 10
Java Interop

Clojure’s Java support is both powerful and lean. It’s powerful, in that it brings the expressiveness of Lisp syntax, plus some syntactic sugar tailored to Java. It’s lean, in that it compiles to bytecode without a translation layer and can thus achieve Java-level performance in nearly every case.

Clojure embraces Java and its libraries. Idiomatic Clojure code calls Java libraries directly and doesn’t try to wrap everything under the sun to look like Lisp. This surprises many new Clojure developers but is very pragmatic. Where Java isn’t broken, Clojure doesn’t fix it. In this chapter, you’ll see how Clojure’s access to Java is convenient, elegant, and fast. In addition, you’ll see how to flip the script and call Clojure from Java.

Clojure’s exception handling is easy to use. Better yet, explicit exception handling is rarely necessary. Clojure’s exception primitives are the same as Java’s. However, Clojure does not require you to deal with checked exceptions and makes it easy to clean up resources using the with-open idiom.

Clojure is fast, unlike many other dynamic languages on the JVM. You can use custom support—for type hints, primitives, and arrays—to cause Clojure’s compiler to generate the same code that a Java compiler would generate.

Clojure is designed to let you get things done and have fun while doing it. However, an important part of getting things done is being able to use your platform to its full potential. Let’s start by seeing how to extend Java interfaces and classes in Clojure.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.236.70