# Clojure - Java Interface

#### Complete Python Prime Pack

9 Courses     2 eBooks

#### Artificial Intelligence & Machine Learning Prime Pack

6 Courses     1 eBooks

#### Java Prime Pack

9 Courses     2 eBooks

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.

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

```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
```
{"ft_lang_label":"__label__en","ft_lang_prob":0.73722196,"math_prob":0.706406,"size":5243,"snap":"2022-40-2023-06","text_gpt3_token_len":1251,"char_repetition_ratio":0.20557359,"word_repetition_ratio":0.15028249,"special_character_ratio":0.24051116,"punctuation_ratio":0.08690745,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99726844,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-10-01T08:33:21Z\",\"WARC-Record-ID\":\"<urn:uuid:1aaa61ff-4839-46d0-bd8b-72137c6e7abf>\",\"Content-Length\":\"38171\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:5db2579f-5c71-4736-a9d4-3412b017e116>\",\"WARC-Concurrent-To\":\"<urn:uuid:09a43589-532a-4cb1-84b7-5ef0bc9b43de>\",\"WARC-IP-Address\":\"168.119.212.138\",\"WARC-Target-URI\":\"https://tutorialspoint.com/clojure/clojure_java_interface.htm\",\"WARC-Payload-Digest\":\"sha1:5YKFSU5NWL6626LJYV6CYDOOM2QB4KTB\",\"WARC-Block-Digest\":\"sha1:M6PUZJ3AM7HIMNX7SVSJH2XD5LGSP6GW\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-40/CC-MAIN-2022-40_segments_1664030335573.50_warc_CC-MAIN-20221001070422-20221001100422-00710.warc.gz\"}"}