rss
twitter
Find out what I'm doing, Follow Me :)

CF9: Datatype of sum of two integer

2
 

I had an interesting observation about datatype conversion in CF9 when dealing with an error in ORM. I was getting an error that said can't use double for an integer field type (something like that). Now, I know that ColdFusion is datatype "independent". So, all simple variables are of datatype "String". Given that, when we do:

 

<cfset myVar = 1 />
<cfdump var="#getMetaData(myVar).toString()#" />

 

The result is "class java.lang.string". Now, if we perform a sum:

 

<cfset myVar = 1 + 1 />
<cfdump var="#getMetaData(myVar).toString()#" />

 

The result is "class java.lang.Double". I expected it to be integer. I was assuming that on the Java side they will get converted to integer. So, next I tried:

 

<cfset myVar = javaCast("int",1) + javaCast("int",1) />

 

And the result was still "class java.lang.Double". 

So, is sum of two integer result in a "Double"? I will let people who are more intelligent than me answer and explain that to me.

But, in the mean time it causes issue when dealing with ORM. I was doing something like this:

 

var hql = "SELECT IsNull(MAX(sortOrder),0) FROM " & arguments.entity.getEntityName() ;
var newSortOrder = ORMExecuteQuery(hql,true) + 1;
arguments.entity.setSortOrder(newSortOrder);

 

This was throwing an error because the datatype of sortOrder field is integer and the type of newSortOrder was "Double". The error can be fixed by using javaCast("int",newSortOrder), but I just changed my select to "IsNull(MAX(sortOrder),0)+1" and it returned me a integer.

Anyone care to explain me why sum of two integer results in a Double in ColdFusion?