- Clojure - Libraries
- Clojure - Automated Testing
- Clojure - Applications
- Clojure - Concurrent Programming
- Clojure - Java Interface
- Clojure - Databases
- Clojure - Reference Values
- Clojure - Macros
- Clojure - Watchers
- Clojure - Agents
- Clojure - StructMaps
- Clojure - Metadata
- Clojure - Atoms
- Clojure - Date & Time
- Clojure - Destructuring
- Clojure - Predicates
- Clojure - Regular Expressions
- Clojure - Sequences
- Clojure - Exception Handling
- Clojure - Namespaces
- Clojure - Maps
- Clojure - Vectors
- Clojure - Sets
- Clojure - Lists
- Clojure - Strings
- Clojure - File I/O
- Clojure - Recursion
- Clojure - Numbers
- Clojure - Functions
- Clojure - Decision Making
- Clojure - Loops
- Clojure - Operators
- Clojure - Variables
- Clojure - Data Types
- Clojure - REPL
- Clojure - Basic Syntax
- Clojure - Environment
- Clojure - Overview
- Clojure - Home
Clojure Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
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 utipze all of the functionapties from Java. In this chapter, let’s discuss the correlation between Clojure and Java.
Calpng 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
Calpng 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 pke 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 pbraries 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 pbrary. 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_45Advertisements