Book Image

Clojure High Performance Programming, Second Edition - Second Edition

By : Shantanu Kumar
Book Image

Clojure High Performance Programming, Second Edition - Second Edition

By: Shantanu Kumar

Overview of this book

Table of Contents (15 chapters)
Clojure High Performance Programming Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Non-numeric scalars and interning


Strings and characters in Clojure are the same as in Java. The string literals are implicitly interned. Interning is a way of storing only the unique values in the heap and sharing the reference everywhere it is required. Depending on the JVM vendor and the version of Java you use, the interned data may be stored in a string pool, Permgen, ordinary heap, or some special area in the heap marked for interned data. Interned data is subject to garbage collection when not in use, just like ordinary objects. Take a look at the following code:

user=> (identical? "foo" "foo")  ; literals are automatically interned
true
user=> (identical? (String. "foo") (String. "foo"))  ; created string is not interned
false
user=> (identical? (.intern (String. "foo")) (.intern (String. "foo")))
true
user=> (identical? (str "f" "oo") (str "f" "oo"))  ; str creates string
false
user=> (identical? (str "foo") (str "foo"))  ; str does not create string for 1 arg
true...