When Java first started to become mainstream fifteen years ago, C and C++ were dominant languages. Sun Microsystems had to convince potential programmers that Java could interoperate with their existing C and C++ libraries. Sun developed he Java Native Interface which allowed C and C++ developers to call their existing code from Java, albeit in a somewhat clumsy, non-intuitive manner. Developers still had to write some pretty ugly code to make the two languages work together.
Fortunately, Clojure is much easier to use in a mixed Java/Clojure environment than C and C++ were in a mixed Java/C++ environment. Millions of lines of Java code have been written by thousands of programmers for more than the past 20 years. Java interoperability is one of the strongest properties of Clojure and one of the most important areas to understand.
Since Clojure runs on the JVM, it has access to all of Java's rich class library and thousands of open-source libraries developed in Java over the past 20 years. For example, you can create a Java collection and use many of the same functions you would use on any of the built-in Clojure collection types.
First import the class. user=> (import [java.util.HashSet]) nil
Then create an instance of the class using the new function
user=> (def mySet (new java.util.HashSet)) #'user/mySet
To use any of the member functions, call them as you would a standard Clojure function, but with the function name preceded by a single dot.
The Clojure built-in types support standard Java interfaces or the standard boxed versions of the Java primitives. You can use the instance? function in Clojure to verify this.
So if your Java function uses interface arguments like this:
public void functionTest(Map m, List l, Set x){...}
instead of classes like this:
public void functionTest(HashMap m, ArrayList l, HashSet x){...}
you can simply create the built-in Clojure types, use the powerful set of functions for those types and call your Java functions without any Java-specific classes inside your Clojure code. You can also use Clojure collections in place of Java generic collections, without the compile time type safety.
Because of the auto-boxing included as part of Java 1.5, you can pass Clojure integers, doubles and doubles into Java functions that use Java primitive type arguments.
Clojure provides a set of functions to create and manipulate Java arrays. To build an array, of a provided type and size, use the make-array function.