Clojure - Data Types



Clojure offers a wide variety of built-in data types.

Built-in Data Types

Following is a list of data types which are defined in Clojure.

  • Integers − Following are the representation of Integers available in Clojure.

    • Decimal Integers (Short, Long and Int) − These are used to represent whole numbers. For example, 1234.

    • Octal Numbers − These are used to represent numbers in octal representation. For example, 012.

    • Hexadecimal Numbers − These are used to represent numbers in representation. For example, 0xff.

    • Radix Numbers − These are used to represent numbers in radix representation. For example, 2r1111 where the radix is an integer between 2 and 36, inclusive.

  • Floating point

    • The default is used to represent 32-bit floating point numbers. For example, 12.34.

    • The other representation is the scientific notation. For example, 1.35e-12.

  • char − This defines a single character literal. Characters are defined with the backlash symbol. For example, /e.

  • Boolean − This represents a Boolean value, which can either be true or false.

  • String − These are text literals which are represented in the form of chain of characters. For example, “Hello World”.

  • Nil − This is used to represent a NULL value in Clojure.

  • Atom − Atoms provide a way to manage shared, synchronous, independent state. They are a reference type like refs and vars.

Bound Values

Since all of the datatypes in Clojure are inherited from Java, the bounded values are the same as in Java programming language. The following table shows the maximum allowed values for the numerical and decimal literals.

literals Ranges
Short -32,768 to 32,767
int -2,147,483,648 to 2,147,483,647
long -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
float 1.40129846432481707e-45 to 3.40282346638528860e+38
double 4.94065645841246544e-324d to 1.79769313486231570e+308d

Class Numeric Types

In addition to the primitive types, the following object types (sometimes referred to as wrapper types) are allowed.

Name
java.lang.Byte
java.lang.Short
java.lang.Integer
java.lang.Long
java.lang.Float
java.lang.Double

Example

The following program shows a consolidated clojure code to demonstrate the data types in Clojure.

(ns clojure.examples.hello
   (:gen-class))

;; This program displays Hello World
(defn Example []
   ;; The below code declares a integer variable
   (def x 1)
   
   ;; The below code declares a float variable
   (def y 1.25)
   
   ;; The below code declares a string variable
   (def str1 "Hello")
   (println x)
   (println y)
   (println str1))
(Example)

Output

The above program produces the following output.

1
1.25
Hello
Advertisements