Wywoływanie kodu Java z Clojure
Dowiedz się, jak wywoływać metody Java, uzyskiwać dostęp do pól i bezpośrednio tworzyć obiekty z poziomu kodu Clojure.
Wywoływanie kodu Java z Clojure to bezpłatna lekcja Clojure Functional Programming & JVM Backend Development na CoddyKit. To lekcja 1 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Clojure Functional Programming & JVM Backend Development, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Clojure Functional Programming & JVM Backend Development zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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.
Często zadawane pytania
Czy lekcja „Wywoływanie kodu Java z Clojure” jest bezpłatna?
Tak — pełny tekst „Wywoływanie kodu Java z Clojure” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Clojure Functional Programming & JVM Backend Development, przejdź na CoddyKit PRO. Kurs Clojure Functional Programming & JVM Backend Development zawiera 4 lekcji w sumie.
Co nauczysz się w „Wywoływanie kodu Java z Clojure”?
Dowiedz się, jak wywoływać metody Java, uzyskiwać dostęp do pól i bezpośrednio tworzyć obiekty z poziomu kodu Clojure. Ćwiczysz Clojure Functional Programming & JVM Backend Development z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Clojure Functional Programming & JVM Backend Development?
Nie wymagamy żadnego doświadczenia. Clojure Functional Programming & JVM Backend Development w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 1 z 4.
Ile czasu zajmuje lekcja „Wywoływanie kodu Java z Clojure”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Clojure Functional Programming & JVM Backend Development?
Tak. Każda lekcja Clojure Functional Programming & JVM Backend Development zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Wywoływanie kodu Java z Clojure
- Implementowanie interfejsów Java
- Wykorzystanie bibliotek i narzędzi Java
- Praca z kolekcjami i tablicami Javy