0Pricing
Clojure Functional Programming & JVM Backend Development · 강의

Clojure에서 Java 호출하기

Clojure 코드에서 직접 Java 메서드를 호출하고 필드에 접근하며 객체를 인스턴스화하는 방법을 알아봅니다.

Clojure에서 Java 호출하기은(는) CoddyKit의 무료 Clojure Functional Programming & JVM Backend Development 강의입니다. 이것은 4개 중 1번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 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/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Clojure Functional Programming & JVM Backend Development 강의 전체를 잠금 해제할 수 있습니다. Clojure Functional Programming & JVM Backend Development 강의에는 총 4개의 강의가 포함되어 있습니다.

“Clojure에서 Java 호출하기”에서 뭘 배우나요?

Clojure 코드에서 직접 Java 메서드를 호출하고 필드에 접근하며 객체를 인스턴스화하는 방법을 알아봅니다. 브라우저에서 직접 실행하는 실습 코드로 Clojure Functional Programming & JVM Backend Development을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Clojure Functional Programming & JVM Backend Development을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Clojure Functional Programming & JVM Backend Development은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 1번째 강의입니다.

“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(으)로 돌아가기