การเรียกใช้ Java จาก Clojure
ค้นพบวิธีเรียกใช้เมธอด Java เข้าถึงฟิลด์ และสร้างออบเจ็กต์โดยตรงจากโค้ด Clojure
การเรียกใช้ Java จาก Clojure เป็นบทเรียน Clojure Functional Programming & JVM Backend Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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.
คำถามที่พบบ่อย
บทเรียน “การเรียกใช้ Java จาก Clojure” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การเรียกใช้ Java จาก Clojure” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Clojure Functional Programming & JVM Backend Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Clojure Functional Programming & JVM Backend Development มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การเรียกใช้ Java จาก Clojure”
ค้นพบวิธีเรียกใช้เมธอด Java เข้าถึงฟิลด์ และสร้างออบเจ็กต์โดยตรงจากโค้ด Clojure คุณปฏิบัติ Clojure Functional Programming & JVM Backend Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Clojure Functional Programming & JVM Backend Development หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Clojure Functional Programming & JVM Backend Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การเรียกใช้ Java จาก Clojure” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Clojure Functional Programming & JVM Backend Development นี้ได้ไหม
ได้ บทเรียน Clojure Functional Programming & JVM Backend Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การเรียกใช้ Java จาก Clojure
- การใช้งานอินเทอร์เฟซ Java
- การใช้ประโยชน์จากไลบรารีและเครื่องมือ Java
- การทำงานกับคอลเลกชันและอาร์เรย์ของ Java