Clojure 5 Clojure and Java The JVM n

  • Slides: 14
Download presentation
Clojure 5 Clojure and Java

Clojure 5 Clojure and Java

The JVM n n Clojure runs on the JVM (Java Virtual Machine) Clojure can

The JVM n n Clojure runs on the JVM (Java Virtual Machine) Clojure can use Java objects, methods, and fields n n n This includes Java’s collections data types, In general, Clojure’s datatypes are preferable because they are immutable and persistent Possibly the best use of Java in Clojure is to make Swing GUIs 2

Imports n (import-lists*) n n n Only classes can be imported, not entire packages

Imports n (import-lists*) n n n Only classes can be imported, not entire packages n n Imports the named Java classes Example: (import '(java. awt. Color) '(java. awt. Point)) There is no equivalent of import java. util. *; All of java. lang is implicitly imported, along with everything in clojure. core 3

Creating Java objects n n (new Java. Class. Name args*) n Creates and returns

Creating Java objects n n (new Java. Class. Name args*) n Creates and returns a new Java object of the specified class n Syntactic sugar: (Java. Class. Name. args*) n Notice the period, it’s important! Examples: n (def rnd (new java. util. Random)) n (def c (java. awt. Color. 255 128 0)) 4

Calling an object’s instance methods n (. Java. Object method. Name args*) n (.

Calling an object’s instance methods n (. Java. Object method. Name args*) n (. method. Name Java. Object args*) n n Calls the Java object’s method with the supplied arguments Examples: n n (. rnd next. Int 10) (. next. Int rnd 10) 5

Accessing instance variables n n n (. Java. Object field. Name) Returns the value

Accessing instance variables n n n (. Java. Object field. Name) Returns the value in the named field of the Java object Example: n After (def p (new java. awt. Point 20 40)), (. p x) will return 20. 6

Calling static methods n n n (Java. Class/static. Method. Name args*) Calls the static

Calling static methods n n n (Java. Class/static. Method. Name args*) Calls the static method of the class Example: (System/current. Time. Millis) 7

Accessing static variables n n n (Java. Class/static. Method. Name args*) Returns the value

Accessing static variables n n n (Java. Class/static. Method. Name args*) Returns the value in the static field of the class Example: (java. awt. Color/CYAN) 8

Making multiple calls to an object n n n doto(Java. Instance. Or. Class method.

Making multiple calls to an object n n n doto(Java. Instance. Or. Class method. Calls*) Uses the Java object or class as the receiver for any number of method calls Example: n (doto new java. util. Hash. Map) (. put "a" 1) (. put "b" 2) ) 9

Proxy n n n (proxy [Class? Interfaces*] [constructor. Args*] functions*) Creates a Clojure class

Proxy n n n (proxy [Class? Interfaces*] [constructor. Args*] functions*) Creates a Clojure class to do the work that would be done by a Java class in Java The optional Class? parameter names the superclass (and if omitted, defaults to Object) If the class’s constructor requires parameters, those are provided in the second vector, otherwise the second vector is empty, [] The functions are usual Clojure functions (omitting the defn), and have the form (name [params*] body) or (name ([params*] body) ([params+] body). . . ) 10

Example proxy n (. convert-button (add. Action. Listener (proxy [Action. Listener] [] (action. Performed

Example proxy n (. convert-button (add. Action. Listener (proxy [Action. Listener] [] (action. Performed [evt] (let [c (Double/parse. Double (. temp-text (get. Text)))] (. fahrenheit-label (set. Text (str (+ 32 (* 1. 8 c)) " Fahrenheit")))) 11

In addition… n n There also functions for dealing with Java arrays, and for

In addition… n n There also functions for dealing with Java arrays, and for treating Java methods as first-class objects These are not covered here 12

I’m thinking of something. A. B. C. Animal Vegetable Mineral 13

I’m thinking of something. A. B. C. Animal Vegetable Mineral 13

The End 14

The End 14