0Pricing
Clojure Functional Programming & JVM Backend Development · レッスン

ClojureからJavaを呼び出す

Clojureコードから直接Javaのメソッドを呼び出し、フィールドにアクセスし、オブジェクトを生成する方法を学びます。

「ClojureからJavaを呼び出す」はCoddyKit上の無料Clojure Functional Programming & JVM Backend Developmentレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはClojure Functional Programming & JVM Backend Development学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Clojure Functional Programming & JVM Backend Developmentコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Clojure & Java: Best Friends!

Clojure runs on the Java Virtual Machine (JVM), which means it can use all the amazing Java libraries directly! This is called Java Interop.

  • Access Java classes and methods.
  • Leverage the vast Java ecosystem.
  • Combine Clojure's power with Java's stability.

This lesson will show you how to call Java code from Clojure.

Creating Java Objects

To use a Java class, you often need to create an instance of it. In Clojure, you can do this using a special syntax.

  • Use (new ClassName args) or the shorthand (ClassName. args).
  • The . after the class name is a convenient way to call a constructor.

Example: (java.util.Date.) creates a new Date object.

Demo: New Date Object

Try creating a new java.util.Date object. It will represent the current date and time.

(ns my-app.core
  (:gen-class))

(defn -main
  "Creates and prints a new Java Date object."
  [& args]
  (let [current-date (java.util.Date.)]
    (println "Current date:" current-date)))

Calling Static Java Methods

Static methods belong to the class itself, not to a specific object instance. You can call them directly on the class.

  • Syntax: (ClassName/staticMethodName args)
  • The / separates the class name from the static method name.

Example: (java.lang.Math/random) calls the random static method of the Math class.

Demo: Math.random()

Let's get a random number using Java's Math/random static method. It returns a double between 0.0 (inclusive) and 1.0 (exclusive).

(ns my-app.core
  (:gen-class))

(defn -main
  "Generates and prints a random number using Java's Math/random."
  [& args]
  (let [rand-num (java.lang.Math/random)]
    (println "Random number:" rand-num)))

Calling Instance Java Methods

Once you have an object, you can call its instance methods. These methods operate on that specific object's data.

  • Syntax: (.methodName instance args) or (. instance methodName args).
  • The . before the method name indicates an instance method call.

Example: (.getTime my-date) calls the getTime method on the my-date object.

Demo: Date.getTime()

Create a Date object and then call its getTime() method. This method returns the number of milliseconds since January 1, 1970, 00:00:00 GMT.

(ns my-app.core
  (:gen-class))

(defn -main
  "Creates a Date object and calls its getTime() method."
  [& args]
  (let [current-date (java.util.Date.)
        time-in-millis (.getTime current-date)]
    (println "Current date:" current-date)
    (println "Milliseconds since epoch:" time-in-millis)))

Accessing Java Fields

Java objects can have fields (variables) that store data. You can access these fields directly from Clojure.

  • Syntax: (.-fieldName instance) for getting a field's value.
  • For static fields, use (.-staticFieldName ClassName).

Example: (.-out System) accesses the out static field of the System class, which is a PrintStream.

Demo: System.out Field

The java.lang.System class has a static field named out, which is a PrintStream object. We can access it to print to the console.

(ns my-app.core
  (:gen-class))

(defn -main
  "Accesses System.out and prints a message using its println method."
  [& args]
  (let [out-stream (.-out java.lang.System)]
    (.println out-stream "Hello from System.out!")))

Quick Check: Java Interop

You've learned how to interact with Java classes. Which Clojure expression uses the ClassName. shorthand to instantiate a java.io.File object for the path "myfile.txt"?

Recap: Java Interop Basics

Great job! You've learned the fundamentals of interacting with Java from Clojure:

  • Instantiate objects: Use (ClassName. args) or (new ClassName args).
  • Call static methods: Use (ClassName/staticMethodName args).
  • Call instance methods: Use (.methodName instance args) or (. instance methodName args).
  • Access fields: Use (.-fieldName instance).

This powerful capability allows you to leverage the entire Java ecosystem within your Clojure projects. Next, we'll explore implementing Java interfaces.

よくある質問

「ClojureからJavaを呼び出す」レッスンは無料ですか?

はい。「ClojureからJavaを呼び出す」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Clojure Functional Programming & JVM Backend Developmentコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Clojure Functional Programming & JVM Backend Developmentコースには全4レッスンが含まれています。

「ClojureからJavaを呼び出す」で何を学びますか?

Clojureコードから直接Javaのメソッドを呼び出し、フィールドにアクセスし、オブジェクトを生成する方法を学びます。 ブラウザで直接実行するハンズオンコードでClojure Functional Programming & JVM Backend Developmentを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Clojure Functional Programming & JVM Backend Developmentを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのClojure Functional Programming & JVM Backend Developmentは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「ClojureからJavaを呼び出す」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このClojure Functional Programming & JVM Backend Developmentレッスンでコードを書いて実行できますか?

はい。すべてのClojure Functional Programming & JVM Backend Developmentレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. ClojureからJavaを呼び出す
  2. Javaインターフェースの実装
  3. Javaライブラリとツールの活用
  4. Javaコレクションと配列の操作
← Clojure Functional Programming & JVM Backend Developmentに戻る