Data Types: Basics
Basic Data Types
Code
(ns data-types-basic.core)
;; ---
;; Basic data types
;; ---
;; Integers
42
;; => 42
;; Float
3.14
;; => 3.14
36786883868216818816N
;; => 36786883868216818816N
3.14159265358M
;; => 3.14159265358M
22/7
;; => 22/7
;; Booleans
true
;; => true
false
;; => false
;; Character
\a
;; => \a
\b
;; => \b
\
;; => \space
;; Strings
"abcdef123456"
;; => "abcdef123456"
;; Keywords
:my-keyword
;; => :my-keyword
nil
;; => nilExplanation
Explanation of the Clojure code:
- Integers:
- The code
42represents an integer with the value 42. - The
=> 42comment indicates that the result of evaluating the code is 42.
- The code
- Float:
- The code
3.14represents a floating-point number with the value 3.14. - The
=> 3.14comment indicates that the result of evaluating the code is 3.14.
- The code
- BigInt:
- The code
36786883868216818816Nrepresents a BigInt, which is a way to represent arbitrarily large integers in Clojure. - The
=> 36786883868216818816Ncomment indicates that the result of evaluating the code is 36786883868216818816N.
- The code
- BigDecimal:
- The code
3.14159265358Mrepresents a BigDecimal, which is a way to represent arbitrary-precision decimal numbers in Clojure. - The
=> 3.14159265358Mcomment indicates that the result of evaluating the code is 3.14159265358M.
- The code
- Ratios:
- The code
22/7represents a ratio, which is a way to represent fractions in Clojure. - The
=> 22/7comment indicates that the result of evaluating the code is 22/7.
- The code
- Booleans:
- The code
truerepresents the boolean value true. - The
=> truecomment indicates that the result of evaluating the code is true. - The code
falserepresents the boolean value false. - The
=> falsecomment indicates that the result of evaluating the code is false.
- The code
- Character:
- The code
\\arepresents the character ‘a’. - The
=> \\acomment indicates that the result of evaluating the code is the character ‘a’. - The code
\\brepresents the character ‘b’. - The
=> \\bcomment indicates that the result of evaluating the code is the character ‘b’. - The code
\\\\represents the character ‘'. - The
=> \\spacecomment indicates that the result of evaluating the code is the character ‘\space’.
- The code
- Strings:
- The code
"abcdef123456"represents a string with the value “abcdef123456”. - The
=> "abcdef123456"comment indicates that the result of evaluating the code is the string “abcdef123456”.
- The code
- Keywords:
- The code
:my-keywordrepresents a keyword, which is a unique identifier in Clojure. - The
=> :my-keywordcomment indicates that the result of evaluating the code is the keyword :my-keyword.
- The code
- nil:
- The code
nilrepresents the absence of a value or the null value in Clojure. - The
=> nilcomment indicates that the result of evaluating the code is nil.
- The code
This code demonstrates the basic data types in Clojure, including integers, floats, BigInts, BigDecimals, ratios, booleans, characters, strings, keywords, and nil. A specific syntax represents each data type, and the comments indicate the result of evaluating the code. Understanding these data types is essential for working with data in Clojure.
Underlying Class Implementation
Code
(ns data-types-basic.core)
;; ---
;; Underlying Class
;; ---
(class 42)
;; => java.lang.Long
(class 3.14)
;; => java.lang.Double
(class 36786883868216818816N)
;; => clojure.lang.BigInt
(class 3.14159265358M)
;; => java.math.BigDecimal
(class 22/7)
;; => clojure.lang.Ratio
(class true)
;; => java.lang.Boolean
(class false)
;; => java.lang.Boolean
(class \a)
;; => java.lang.Character
(class "abcdef123456")
;; => java.lang.String
(class :my-keyword)
;; => clojure.lang.Keyword
(class nil)
;; => nilExplanation
Explanation of the Clojure code:
- The code
(class 42)returns the underlying class of the value 42. - The
=> java.lang.Longcomment indicates that the result of evaluating the code is the classjava.lang.Long. - The code
(class 3.14)returns the underlying class of the value 3.14. - The
=> java.lang.Doublecomment indicates that the result of evaluating the code is the classjava.lang.Double. - The code
(class 36786883868216818816N)returns the underlying class of the value 36786883868216818816N. - The
=> clojure.lang.BigIntcomment indicates that the result of evaluating the code is the classclojure.lang.BigInt. - The code
(class 3.14159265358M)returns the underlying class of the value 3.14159265358M. - The
=> java.math.BigDecimalcomment indicates that the result of evaluating the code is the classjava.math.BigDecimal. - The code
(class 22/7)returns the underlying class of the value 22/7. - The
=> clojure.lang.Ratiocomment indicates that the result of evaluating the code is the classclojure.lang.Ratio. - The code
(class true)returns the underlying class of the value true. - The
=> java.lang.Booleancomment indicates that the result of evaluating the code is the classjava.lang.Boolean. - The code
(class false)returns the underlying class of the value false. - The
=> java.lang.Booleancomment indicates that the result of evaluating the code is the classjava.lang.Boolean. - The code
(class \\a)returns the value ‘a’ underlying class. - The
=> java.lang.Charactercomment indicates that the result of evaluating the code is the classjava.lang.Character. - The code
(class "abcdef123456")returns the underlying class of the value “abcdef123456”. - The
=> java.lang.Stringcomment indicates that the result of evaluating the code is the classjava.lang.String. - The code
(class :my-keyword)returns the underlying class of the value :my-keyword. - The
=> clojure.lang.Keywordcomment indicates that the result of evaluating the code is the classclojure.lang.Keyword. - The code
(class nil)returns the underlying class of the value nil. - The
=> nilcomment indicates that the result of evaluating the code is nil.
This code demonstrates how to determine the underlying class of values in Clojure. The class function is used to retrieve the class of a value. The comments indicate the result of evaluating the code, which is the class of the corresponding value. Understanding the underlying class of values can be helpful when working with Java interop or polymorphic behavior in Clojure.