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.
I set up these values:
Launch code in new window » Download code as text file »
... and outputted the data types:
Launch code in new window » Download code as text file »
... 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
I wanted to see if it mattered to declare the actual scope. So, I set up these variables:
Launch code in new window » Download code as text file »
... and outputted the data types:
Launch code in new window » Download code as text file »
... 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
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:
Launch code in new window » Download code as text file »
... and outputted the data types:
Launch code in new window » Download code as text file »
... 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.
Download Code Snippet ZIP File
Comments (3) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
Skin Spider : Pseudo Application.cfc Events : OnContentStart() And OnContentEnd()
ColdFusion Query of Queries Unexpected Data Type Conversion
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.
Posted by James on Nov 2, 2006 at 3:11 PM
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.
Posted by Ben Nadel on Nov 3, 2006 at 7:21 AM
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.
Posted by jM on Mar 25, 2007 at 6:09 PM