Why does (int 10) not produce an instance of type java.lang.Integer?
; why Long here?
=> (type (int 10))
; java.lang.Long
; this one is also Long, why not java.lang.Number?
=> (type (num 10))
; java.lang.Long
=> (type (double 10))
; java.lang.Double
=> (type (long 10))
; java.lang.Long
=> (type (float 10))
; java.lang.Float
=> (type (short 10))
; java.lang.Short
=> (type (bigint 10))
; clojure.lang.BigInt
=> (type (bigdec 10))
; java.math.BigDecimal
=> (type (boolean 10))
; java.lang.Boolean
=> (type (char 10))
; java.lang.Character
=> (type (byte 10))
; java.lang.Byte
Clojure deals only with
longintegers internally.(int)is used to cast alongto anintfor calling Java methods that expect anintargument.In this case
(int 10)does indeed return a Javaint, but Clojure then promotes theintback to along.(type)uses(class)to find out the type of its argument (in this case), and therefore thelonggets boxed into ajava.lang.Long.You can produce
java.lang.Integerby using one of thejava.lang.Integerconstructors or factory methods:(num)will upcast its argument to the abstract classjava.lang.Number, but(type)will return the actual type of its argument, i.e.java.lang.Longagain.