-
Book Overview & Buying
-
Table Of Contents
Clojure for Java Developers
Now is when everything you know about Java pays off; even the list forms that you saw earlier implement the java.util.List interface. Clojure was designed to be embeddable and to have a great integration with the host platform, so it's only natural that you can use everything you already know about Java types and objects.
There are two data types in Clojure: scalars and collections.
In every language you need primitive types; you use them in everyday life as they represent numbers, strings, and Booleans. These primitive types are called scalars in the Clojure world.
Clojure has a couple of very interesting types like ratios and keywords. In the following table, you get to know the different types of scalars, how they compare to Java and a simple example of how to use each of them.
|
Clojure data type |
Java data type |
Sample |
Description |
|---|---|---|---|
|
String |
String |
"This is a string" "This is a multiline string" |
A string of characters; in Clojure you can use multiline strings without a problem |
|
Boolean |
Boolean |
true false |
Literal Boolean values |
|
Character |
Character |
|
Character values, they are |
|
Keywords |
Doesn't exist in java |
|
They evaluate themselves and they are often used as keys. They are also functions that look for themselves in a map. |
|
Number |
Numbers are automatically handled as |
|
It is important to remember the trade-offs of Java numbers, if precision is important, you should always use big decimals and |
|
Ratio |
Doesn't exist |
|
Clojure provides great numerical precision; if necessary it can retain the ration and execute exact operation. The tradeoff when using ratios is speed. |
|
Symbol |
Doesn't exist |
some-name |
Symbols are identifiers in Clojure, very similar to a variable name in Java. |
|
nil |
null |
nil |
The null value |
|
Regular expressions |
|
|
Regular expressions, in Clojure you get free syntax to define regular expressions, but in the end it is a plain old Java reggae Pattern |
In Clojure there are two types of collections: sequential and associative collections. Sequential are things you can iterate, such as lists. Associative collections are maps, sets, and things you can access by a certain index. Clojure's collections are fully compatible with Java and it can even implement the java.util interfaces, such as java.util.List and java.util.Map.
One of the main characteristics of collections in Clojure is that they are immutable; it has a lot of benefits that we'll see later.
Let's have a look at the characteristics of each collection data type available in Clojure and compare them with Java with the help of a sample (in Clojure) and its description.
|
Clojure data type |
Java data type |
Sample |
Description |
|---|---|---|---|
|
|
|
|
A simple list, notice the quote character before the list, if you don't specify it Clojure will try to evaluate the form as an instruction |
|
|
|
|
It is the main workhorse in Clojure, it is similar to an array because you can access elements in a random order |
|
|
|
|
A normal Java hash set |
|
|
|
|
A Clojure map |
Change the font size
Change margin width
Change background colour