ColdFusion Data Types From Different Sources (And How ColdFusion Sees Them)
Most things in ColdFusion appear to be strings, but do you ever wonder what they are underneath? Yeah, me too. It's good to know as it can cause issue when sending data as arguments to Java methods or when attempting to use the underlying Java methods. I tried setting / paraming data in various scope to see how ColdFusion interpreted them.
VARIABLES Scope
I set up these values:
<cfset a = "0" />
<cfset b = 0 />
<cfparam name="c" type="numeric" default="0" />
<cfparam name="d" type="boolean" default="0" />
<cfparam name="e" type="date" default="#Now()#" />
<cfparam name="f" type="email" default="sarah@girls-who-code.com" />
<cfparam name="g" type="eurodate" default="02/11/06" />
<cfparam name="h" type="float" default="0.0" />
<cfparam name="i" type="GUID" default="AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA" />
<cfparam name="j" type="integer" default="0" />
<cfparam name="k" type="ssn" default="105-00-1234" />
<cfparam name="l" type="telephone" default="2126911134" />
<cfparam name="m" type="time" default="09:00:00" />
<cfparam name="n" type="URL" default="http://www.bennadel.com" />
<cfparam name="o" type="XML" default="<dude>Ben</dude>" />
... and outputted the data types:
A: #a.GetClass().GetName()#
B: #b.GetClass().GetName()#
C: #c.GetClass().GetName()#
D: #d.GetClass().GetName()#
E: #e.GetClass().GetName()#
F: #f.GetClass().GetName()#
G: #g.GetClass().GetName()#
H: #h.GetClass().GetName()#
I: #i.GetClass().GetName()#
J: #j.GetClass().GetName()#
K: #k.GetClass().GetName()#
L: #l.GetClass().GetName()#
M: #m.GetClass().GetName()#
N: #n.GetClass().GetName()#
O: #o.GetClass().GetName()#
... and got the following output:
A: java.lang.String
B: java.lang.String
C: java.lang.String
D: java.lang.String
E: coldfusion.runtime.OleDateTime
F: java.lang.String
G: java.lang.String
H: java.lang.String
I: java.lang.String
J: java.lang.String
K: java.lang.String
L: java.lang.String
M: java.lang.String
N: java.lang.String
O: java.lang.String
FORM Scope
I wanted to see if it mattered to declare the actual scope. So, I set up these variables:
<cfset FORM.a = "0" />
<cfset FORM.b = 0 />
<cfparam name="FORM.c" type="numeric" default="0" />
<cfparam name="FORM.d" type="boolean" default="0" />
<cfparam name="FORM.e" type="date" default="#Now()#" />
<cfparam name="FORM.f" type="email" default="sarah@girls-who-code.com" />
<cfparam name="FORM.g" type="eurodate" default="02/11/06" />
<cfparam name="FORM.h" type="float" default="0.0" />
<cfparam name="FORM.i" type="GUID" default="AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA" />
<cfparam name="FORM.j" type="integer" default="0" />
<cfparam name="FORM.k" type="ssn" default="105-00-1234" />
<cfparam name="FORM.l" type="telephone" default="2126911134" />
<cfparam name="FORM.m" type="time" default="09:00:00" />
<cfparam name="FORM.n" type="URL" default="http://www.bennadel.com" />
<cfparam name="FORM.o" type="XML" default="<dude>Ben</dude>" />
... and outputted the data types:
A: #FORM[ "a" ].GetClass().GetName()#
B: #FORM[ "b" ].GetClass().GetName()#
C: #FORM[ "c" ].GetClass().GetName()#
D: #FORM[ "d" ].GetClass().GetName()#
E: #FORM[ "e" ].GetClass().GetName()#
F: #FORM[ "f" ].GetClass().GetName()#
G: #FORM[ "g" ].GetClass().GetName()#
H: #FORM[ "h" ].GetClass().GetName()#
I: #FORM[ "i" ].GetClass().GetName()#
J: #FORM[ "j" ].GetClass().GetName()#
K: #FORM[ "k" ].GetClass().GetName()#
L: #FORM[ "l" ].GetClass().GetName()#
M: #FORM[ "m" ].GetClass().GetName()#
N: #FORM[ "n" ].GetClass().GetName()#
O: #FORM[ "o" ].GetClass().GetName()#
... and got the following output:
A: java.lang.String
B: java.lang.String
C: java.lang.String
D: java.lang.String
E: coldfusion.runtime.OleDateTime
F: java.lang.String
G: java.lang.String
H: java.lang.String
I: java.lang.String
J: java.lang.String
K: java.lang.String
L: java.lang.String
M: java.lang.String
N: java.lang.String
O: java.lang.String
CFQUERY Scope
I wanted to see what kind of data was coming out of the database, well rather, the Query of Queries database. So, I set up these variables:
<cfset qTemp = QueryNew( "id" ) />
<cfset QueryAddRow( qTemp ) />
<cfquery name="qTemp" dbtype="query">
SELECT
( CAST( '0' AS BINARY ) ) AS a,
( CAST( 0 AS BIGINT ) ) AS b,
( CAST( 0 AS BIT ) ) AS c,
( CAST( 0 AS DATE ) ) AS d,
( CAST( 0 AS DECIMAL ) ) AS e,
( CAST( 0 AS INTEGER ) ) AS f,
( CAST( 0 AS TIME ) ) AS g,
( CAST( 0 AS TIMESTAMP ) ) AS h,
( CAST( 0 AS VARCHAR ) ) AS i
FROM
qTemp
</cfquery>
... and outputted the data types:
A: #qTemp[ "a" ][ 1 ].GetClass().GetName()#
B: #qTemp[ "b" ][ 1 ].GetClass().GetName()#
C: #qTemp[ "c" ][ 1 ].GetClass().GetName()#
D: #qTemp[ "d" ][ 1 ].GetClass().GetName()#
E: #qTemp[ "e" ][ 1 ].GetClass().GetName()#
F: #qTemp[ "f" ][ 1 ].GetClass().GetName()#
G: #qTemp[ "g" ][ 1 ].GetClass().GetName()#
H: #qTemp[ "h" ][ 1 ].GetClass().GetName()#
I: #qTemp[ "i" ][ 1 ].GetClass().GetName()#
... and got the following output:
A: [B
B: java.lang.Long
C: java.lang.Integer
D: java.sql.Date
E: java.math.BigDecimal
F: java.lang.Integer
G: java.sql.Time
H: java.sql.Timestamp
I: java.lang.String
As you can see, pretty much all ColdFusion-defined variables are stored as strings (for simple values). For some reason, it stored DATE as a DATETIME object, but NOT TIME as a DATETIME object... go figure. On the other side of the wall, all the data that comes out of a database is very Java data type oriented.
Good stuff to know.
Want to use code from this post? Check out the license.
Reader Comments
Also useful to know that if you were to do something like:
<cfset c = c + 1/>
And then call c.GetClass().GetName(), you'd end up with java.lang.Double . It's caught me out a couple of times when passing values into java methods that are of type int and obviously I was passing a double without realising.
James,
Nice catch! Yeah, that is why I now try to make it a habit of always always always using JavaCast() when I am sending data to a Java method. The problem is that so often, it will take a string just fine and I have gotten lazy about using it for things like ReplaceAll() which work fine like all the time.
For some reason, it stored DATE as a DATETIME object, but NOT TIME as a DATETIME object
Not exactly. They were converted to java.sql.Date and java.sql.Time. I think you're confusing database data types and java.sql types.