পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Friday, January 27, 2017

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

No comments:

Post a Comment