CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-clojure--clojure

Clojure core environment and runtime library providing a dynamic, general-purpose programming language on the JVM.

Pending
Overview
Eval results
Files

java-interop.mddocs/

Java Interoperability

Seamless integration with Java classes, methods, and the broader JVM ecosystem. Clojure provides comprehensive interop capabilities for working with existing Java libraries.

Capabilities

Java Object Creation

Methods for creating and instantiating Java objects.

(new ClassName args*)
;; Special form: creates new instance of Java class
;; Returns: Java object instance

(ClassName. args*)
;; Macro: alternative syntax for new (note the dot)
;; Returns: Java object instance

;; Constructor interop examples:
(new java.util.Date)              ; Create new Date
(java.util.Date.)                 ; Alternative syntax
(new String "hello")              ; Create String from char array
(String. "hello")                 ; Alternative syntax

Method Invocation

Calling methods on Java objects and classes.

(.method object args*)
;; Special form: calls instance method on object
;; Returns: method return value

(. object method args*)
;; Special form: alternative method call syntax
;; Returns: method return value

(ClassName/staticMethod args*)
;; Special form: calls static method on class
;; Returns: method return value

(. ClassName staticMethod args*)
;; Special form: alternative static method syntax
;; Returns: method return value

;; Method chaining with .. macro
(.. object method1 method2 method3)
;; Macro: chains method calls
;; Equivalent to (.method3 (.method2 (.method1 object)))
;; Returns: final method return value

Field Access

Accessing Java object fields and class fields.

(.field object)
;; Special form: gets instance field value
;; Returns: field value

(.-field object)  
;; Special form: alternative field access syntax
;; Returns: field value

(ClassName/staticField)
;; Special form: gets static field value
;; Returns: field value

(. ClassName staticField)
;; Special form: alternative static field syntax  
;; Returns: field value

(set! (.field object) value)
;; Special form: sets instance field (if mutable)
;; Returns: value

(set! (. object field) value)
;; Special form: alternative field setting syntax
;; Returns: value

Class and Type Operations

Working with Java classes and type information.

(class object)
;; Function: returns Class object for object
;; Returns: java.lang.Class

(type object)
;; Function: returns type of object (Class or Clojure type)
;; Returns: java.lang.Class or clojure type

(instance? Class object)
;; Function: tests if object is instance of Class
;; Returns: boolean

(isa? child parent)
;; Function: tests class/interface hierarchy relationship
;; Returns: boolean

(ancestors Class)
;; Function: returns set of all ancestor classes/interfaces
;; Returns: set of java.lang.Class

(descendants Class)
;; Function: returns set of all descendant classes
;; Returns: set of java.lang.Class

(supers Class)
;; Function: returns set of immediate superclass/interfaces
;; Returns: set of java.lang.Class

Class Loading and Importing

Managing Java class imports and loading.

(import & import-lists)
;; Special form: imports Java classes into current namespace
;; import-lists: ClassName or (package.name Class1 Class2 ...)
;; Returns: nil

;; Import examples:
(import 'java.util.Date)               ; Single class
(import '(java.util Date Calendar))    ; Multiple classes from package
(import 'java.util.*)                  ; Not supported - must be explicit

;; In namespace declaration:
(ns my.namespace
  (:import [java.util Date Calendar]
           [java.io File FileReader]))

Array Operations

Working with Java arrays.

(make-array Class dims*)
;; Function: creates multi-dimensional array
;; Returns: Java array

(object-array size-or-seq)
;; Function: creates Object array
;; Returns: Object[]

(boolean-array size-or-seq)
;; Function: creates boolean array  
;; Returns: boolean[]

(byte-array size-or-seq)
;; Function: creates byte array
;; Returns: byte[]

(char-array size-or-seq)
;; Function: creates char array
;; Returns: char[]

(short-array size-or-seq)
;; Function: creates short array
;; Returns: short[]

(int-array size-or-seq)
;; Function: creates int array
;; Returns: int[]

(long-array size-or-seq)
;; Function: creates long array
;; Returns: long[]

(float-array size-or-seq)
;; Function: creates float array
;; Returns: float[]

(double-array size-or-seq)
;; Function: creates double array
;; Returns: double[]

(aclone array)
;; Function: shallow clone of array
;; Returns: cloned array

(alength array)
;; Function: returns length of array
;; Returns: int

(aget array idx & idxs)
;; Function: gets array element at indices
;; Returns: array element

(aset array idx val)
(aset array idx idx2 val)
;; Function: sets array element at indices
;; Returns: val

(to-array coll)
;; Function: converts collection to Object array
;; Returns: Object[]

(to-array-2d coll)
;; Function: converts collection of collections to 2D array
;; Returns: Object[][]

(into-array type coll)
;; Function: converts collection to typed array
;; Returns: typed array

Type Coercion

Converting between Clojure and Java types.

(boolean x)
;; Function: coerces to boolean (false for nil/false, true otherwise)
;; Returns: boolean

(byte x)
;; Function: coerces to byte
;; Returns: byte

(char x)
;; Function: coerces to char
;; Returns: char

(short x)
;; Function: coerces to short
;; Returns: short

(int x)
;; Function: coerces to int
;; Returns: int

(long x)
;; Function: coerces to long
;; Returns: long

(float x)
;; Function: coerces to float
;; Returns: float

(double x)
;; Function: coerces to double
;; Returns: double

(bigint x)
;; Function: coerces to BigInteger
;; Returns: java.math.BigInteger

(bigdec x)
;; Function: coerces to BigDecimal
;; Returns: java.math.BigDecimal

Proxy and Interface Implementation

Creating objects that implement Java interfaces or extend classes.

(proxy [Class* interfaces*] [super-ctor-args*] 
  (method-name [args*] body)*)
;; Macro: creates object implementing interfaces/extending class
;; Returns: proxy object

(reify interfaces*
  (method-name [this args*] body)*)  
;; Macro: creates object implementing interfaces (more efficient than proxy)
;; Returns: reified object

;; Proxy example:
(proxy [java.io.InputStream] []
  (read [] -1))

;; Reify example:  
(reify java.util.Comparator
  (compare [this a b] 
    (Integer/compare a b)))

Exception Handling with Java

Working with Java exceptions in Clojure.

;; Standard try-catch works with Java exceptions
(try
  (java-operation-that-might-throw)
  (catch java.io.IOException e
    (handle-io-exception e))
  (catch Exception e
    (handle-general-exception e)))

;; Access exception methods
(.getMessage exception)
(.printStackTrace exception)
(.getCause exception)

Usage Examples:

;; Object creation
(def date (new java.util.Date))
(def list (java.util.ArrayList.))

;; Method calls
(.toString date)                      ; => "Thu Sep 05 19:32:00 UTC 2025"
(.add list "hello")                   ; => true
(.size list)                          ; => 1

;; Static methods  
(System/currentTimeMillis)            ; => 1725564720000
(Math/sqrt 16)                        ; => 4.0

;; Field access
System/out                            ; => PrintStream
(.length "hello")                     ; => 5

;; Method chaining
(.. "hello world" 
    (toUpperCase)
    (substring 0 5))                  ; => "HELLO"

;; Arrays
(def arr (int-array [1 2 3 4]))
(alength arr)                         ; => 4
(aget arr 0)                          ; => 1
(aset arr 0 10)                       ; => 10

;; Type checking
(class "hello")                       ; => java.lang.String
(instance? String "hello")            ; => true
(instance? Number 42)                 ; => true

;; Interface implementation
(def my-comparator
  (reify java.util.Comparator
    (compare [this a b]
      (Integer/compare a b))))

(def sorted-list 
  (java.util.TreeSet. my-comparator))

;; Collections interop
(def java-list (java.util.ArrayList. [1 2 3]))
(seq java-list)                       ; => (1 2 3)
(vec java-list)                       ; => [1 2 3]

;; Exception handling
(try
  (Integer/parseInt "not-a-number")
  (catch NumberFormatException e
    (str "Invalid number: " (.getMessage e))))

Install with Tessl CLI

npx tessl i tessl/maven-org-clojure--clojure

docs

concurrency.md

control-flow.md

core-language.md

data-structures.md

data-utilities.md

dev-tools.md

formats.md

index.md

io.md

java-interop.md

math.md

sequences.md

strings.md

testing.md

tile.json