Clojure – Java Interface


Clojure – Java Interface


”;


As we already know, Clojure code runs on the Java virtual environment at the end. Thus it only makes sense that Clojure is able to utilize all of the functionalities from Java. In this chapter, let’s discuss the correlation between Clojure and Java.

Calling Java Methods

Java methods can be called by using the dot notation. An example is strings. Since all strings in Clojure are anyway Java strings, you can call normal Java methods on strings.

An example on how this is done is shown in the following program.

Example

(ns Project
   (:gen-class))
(defn Example []
   (println (.toUpperCase "Hello World")))
(Example)

The above program produces the following output. You can see from the code that if you just call the dot notation for any string method, it will also work in Clojure.

Output

HELLO WORLD

Calling Java Methods with Parameters

You can also call Java methods with parameters. An example on how this is done is shown in the following program.

Example

(ns Project
   (:gen-class))
(defn Example []
   (println (.indexOf "Hello World","e")))
(Example)

The above program produces the following output. You can see from the above code, that we are passing the parameter “e” to the indexOf method. The above program produces the following output.

Output

1

Creating Java Objects

Objects can be created in Clojure by using the ‘new’ keyword similar to what is done in Java.

An example on how this is done is shown in the following program.

Example

(ns Project
   (:gen-class))
(defn Example []
   (def str1 (new String "Hello"))
   (println str1))
(Example)

The above program produces the following output. You can see from the above code, that we can use the ‘new’ keyword to create a new object from the existing String class from Java. We can pass the value while creating the object, just like we do in Java. The above program produces the following output.

Output

Hello

Following is another example which shows how we can create an object of the Integer class and use them in the normal Clojure commands.

Example

(ns Project
   (:gen-class))
(defn Example []
   (def my-int(new Integer 1))
   (println (+ 2 my-int)))
(Example)

The above program produces the following output.

Output

3

Import Command

We can also use the import command to include Java libraries in the namespace so that the classes and methods can be accessed easily.

The following example shows how we can use the import command. In the example we are using the import command to import the classes from the java.util.stack library. We can then use the push and pop method of the stack class as they are.

Example

(ns Project
   (:gen-class))
(import java.util.Stack)
(defn Example []
   (let [stack (Stack.)]
   (.push stack "First Element")
   (.push stack "Second Element")
   (println (first stack))))
(Example)

The above program produces the following output.

Output

First Element

Running Code Using the Java Command

Clojure code can be run using the Java command. Following is the syntax of how this can be done.

java -jar clojure-1.2.0.jar -i main.clj

You have to mention the Clojure jar file, so that all Clojure-based classes will be loaded in the JVM. The ‘main.clj’ file is the Clojure code file which needs to be executed.

Java Built-in Functions

Clojure can use many of the built-in functions of Java. Some of them are −

Math PI function − Clojure can use the Math method to the value of PI. Following is an example code.

Example

(ns Project
   (:gen-class))
(defn Example []
   (println (. Math PI)))
(Example)

The above code produces the following output.

Output

3.141592653589793

System Properties − Clojure can also query the system properties. Following is an example code.

Example

(ns Project
   (:gen-class))
(defn Example []
   (println (.. System getProperties (get "java.version"))))
(Example)

Depending on the version of Java on the system, the corresponding value will be displayed. Following is an example output.

Output

1.8.0_45

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *